Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > nested classes and friend declaration

Reply
Thread Tools

nested classes and friend declaration

 
 
Alexander Stippler
Guest
Posts: n/a
 
      07-02-2003
Hi

what's wrong about the following code? It does not compile.

template <typename T>
class A {
public:
class B;
};

template <typename T>
bool
operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs);

template <typename T>
bool
operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs);

template <typename T>
class A<T>::B
{
public:
friend bool
operator==<>(const B &lhs, const B &rhs);

friend bool
operator!=<>(const B &lhs, const B &rhs);
};

template <typename T>
bool
operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
{
return true;
}

template <typename T>
bool
operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
{
return false;
}

int
main()
{
A<int>::B b;
b == b;

}

regards,
Alex
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2003
"Alexander Stippler" <(E-Mail Removed)-ulm.de> wrote...
> what's wrong about the following code? It does not compile.


Several things are wrong. However, I'd like to know what prevented
you from posting the compiler diagnostic?

>
> template <typename T>
> class A {
> public:
> class B;
> };
>
> template <typename T>
> bool
> operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs);
>
> template <typename T>
> bool
> operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs);
>
> template <typename T>
> class A<T>::B
> {
> public:
> friend bool
> operator==<>(const B &lhs, const B &rhs);


If you need a particular instantiation to be a friend, you expect that
'T' is going to be determined here. It won't. The language does not
allow template argument deduction from nested types in templates. You
have to specify the T:

friend bool operator==<T>(...

..

>
> friend bool
> operator!=<>(const B &lhs, const B &rhs);


Same here.

> };
>
> template <typename T>
> bool
> operator==(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
> {
> return true;
> }
>
> template <typename T>
> bool
> operator!=(const typename A<T>::B &lhs, const typename A<T>::B &rhs)
> {
> return false;
> }
>
> int
> main()
> {
> A<int>::B b;
> b == b;


Now, this is not going to be resolved. Again, because the compiler
does not know how to deduce type arguments from nested types, you
need to help it:

operator==<int>(b,b);

Ugly? Yes. But there is no other way. Perhaps you need to think
of making your operator== and operator!= templates based on B and
not on T:

template<typename B> bool operator==(B const&, B const&);

and then specialise them for A<T>::B if needed...

>
> }
>
> regards,
> Alex



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friend and nested classes Chameleon C++ 1 02-14-2009 07:49 PM
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Forward declaration and nested classes .. Susan Baker C++ 1 07-07-2005 01:25 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM



Advertisments