473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

nested classes and friend declaration

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
Jul 19 '05 #1
1 2791
"Alexander Stippler" <st**@mathematik.uni-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

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert M. Gary | last post by:
I'm curious what the ANSI C++ standard says about nested classes. I'm not able to find where in the ANSI C++ standard this is addressed. The issue is the accessibility of sibling nested classes....
1
by: Chris Schadl | last post by:
Okay, I'm having a bit of a brain-fart and I can't remember how I would do this. Say I have the following: template <typename T1, typename T2> class A; // Forward declaration of A template...
7
by: newbiecpp | last post by:
Why this code cannot be compiled (under VC++ 7.0): class Outer { public: class Inner { public: Inner() : in_data(0) {} void action() { Outer::out_data++; // illegal reference to...
3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
5
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include <iostream> class Outer { private: int...
7
by: Mark P | last post by:
Is this legal and sensible? class Outer { friend struct Inner { ... }; ...
3
by: Mark | last post by:
Hi... Maybe I'm just being especially obtuse today, but I've been perplexed by how some of the protection levels have been working with nested classes in C#. I have a parent class that wants to...
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.