template<typename T> class FooBar {};
template<typename T> class Bar {
friend class FooBar<T>;
};
template<typename T> class Bar2 {
template friend class FooBar<T>;
};
What is the difference between class Bar and Bar2?
template<typename T> class FooBar {};
template<typename T> class Bar {
friend class FooBar<T>;
};
template<typename T> class Bar2 {
template friend class FooBar<T>;
};
What is the difference between class Bar and Bar2?
The second one you have is invalid syntax, according to my compiler. If you change them to:
template<typename T> class FooBar {};
template<typename T> class Bar {
friend class FooBar<T>;
};
template<typename T> class Bar2 {
template<typename T2> friend class FooBar;
};
Then it will compile. The difference is that in Bar<T>
, only FooBar<T>
is a friend; if you have Bar<int>
, only FooBar<int>
is a friend, not FooBar<char>
or any other type but int
. In Bar2<T>
, any type of FooBar
is a friend.
If the Bar2 class definition is modified like below
template<typename T> class Bar2 {
template<typename U> friend class FooBar<U>;
};
then, anytype of FooBar is friends with anytype of Bar2.
However Bar class definition tells that FooBar with type T is friends with Bar of same type. i.e. Bar < char > & FooBar< char > and not Bar< int > & FooBar< char >
Shash