Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 9th, 2008, 11:05 PM
Andy Champ
Guest
 
Posts: n/a
Default What's the correct syntax for a friend with template parameters?

We have a class with something that, simplified, looks like this:

template <typename Tclass foo
{
T beyondAllReaching;

// In VC2005, this works OK
template <typename Ufriend void method(foo<Tl, foo<Ur);

// But VC2008 requires this
template <typename U, class Vfriend void method(U l, V r);
};

// To allow the actual function to access the private data.
template <typename T, typename Uvoid method(foo<Tl, foo<Ur)
{
l.beyondAllReaching = r.beyondAllReaching;
}

It occurs to me writing this that method<int, charis not a friend of
foo<char- so I'm a bit surprised that either works. But that isn't
the main point. I want to know:

- Which is the correct syntax?
- Is there a way to stop a method<string, stringaccessing the privates
of a foo<char>?

Thanks

Andy
  #2  
Old July 9th, 2008, 11:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: What's the correct syntax for a friend with template parameters?

Andy Champ wrote:
Quote:
We have a class with something that, simplified, looks like this:
>
template <typename Tclass foo
{
T beyondAllReaching;
>
// In VC2005, this works OK
template <typename Ufriend void method(foo<Tl, foo<Ur);
>
// But VC2008 requires this
template <typename U, class Vfriend void method(U l, V r);
};
>
// To allow the actual function to access the private data.
template <typename T, typename Uvoid method(foo<Tl, foo<Ur)
This is a function template with 2 template arguments. Shouldn't the
'friend' declaration also have 2 template arguments? That's the reason
for the correct syntax (you marked it as VC2008).
Quote:
{
l.beyondAllReaching = r.beyondAllReaching;
}
>
It occurs to me writing this that method<int, charis not a friend of
foo<char- so I'm a bit surprised that either works.
Actually, that's not true, the friend declaration declares *any*
instantiation of 'method' function template to be a friend of *any*
'foo' class template instantiation.
Quote:
But that isn't
the main point. I want to know:
>
- Which is the correct syntax?
To declare what, exactly?
Quote:
- Is there a way to stop a method<string, stringaccessing the privates
of a foo<char>?
'fraid not. Partial specialisations aren't allowed in friend
declarations. Also, there are no partial specialisations of function
templates.

template<class Tclass foo;
template<class A, class Bvoid method(foo<A>, foo<B>);

template<typename Tclass foo
{
T beyondAllReaching;
template<class S, class Ufriend void method(foo<Sl, foo<Ur);
};

template<class A, class Bvoid method(foo<Afa, foo<Bfb)
{
fa.beyondAllReaching = 0;
fb.beyondAllReaching = 0;

foo<doublefd;
fd.beyondAllReaching = 3.14159; // oops. Didn't want that...
}

int main()
{
foo<intfi;
foo<charfc;

method(fi, fc);
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  #3  
Old July 10th, 2008, 08:15 PM
Andy Champ
Guest
 
Posts: n/a
Default Re: What's the correct syntax for a friend with template parameters?

Victor Bazarov wrote:
Quote:
>
Actually, that's not true, the friend declaration declares *any*
instantiation of 'method' function template to be a friend of *any*
'foo' class template instantiation.
>
Ah. That's not *quite* what I wanted, but that's the language so...

Thanks

Andy
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.