Is there a syntax for friending only certain specializations of a templated class, or do you have to friend all specializations with the likes of:
template<class FooType> friend class Bar;
If it existed, I would be looking for something like:
template<class FooType> friend class Bar<FooType const>;
It seems the specializations have their own "friendable" identity, because they are not friends of each other by default. For instance, getFoo()
here can't be called by the non-const case derived from the const specialization because it is private:
template<class FooType> class Bar;
template<class FooType>
class Bar<FooType const> {
public:
Bar (std::string name) : _foo (name) { }
FooType const * operator->() const
{ return &getFoo(); }
private:
FooType const & getFoo() const
{ return _foo; }
private:
FooType _foo;
};
template<class FooType>
class Bar : public Bar<FooType const> {
public:
Bar (std::string name) : Bar<FooType const> (name) { }
FooType * operator->()
{ return &getFoo(); }
private:
FooType & getFoo()
{ return const_cast<FooType &>(Bar<FooType const>::getFoo()); }
};
You have to add template<class FooType> friend Bar;
to the const specialization.
(As an aside, the tag description for friend is sort of funny.)