Friend in derived template class - compiler broken, worth an FAQ,
or is it just me?
I'm running MSVC, and I haven't tried it on anything else.
This is fine
// Base class
struct Base
{
Base(){};
};
// declare factory function
template <class T> Base* Ctor();
template <typename T> class C: public Base
{
friend Base* Ctor<T>();
private:
// Private constructor (can only make this from a friend)
C() {};
};
template <class T> Base* Ctor()
{
return new C<T>();
}
int main(int argc, char* argv[])
{
Base* p = Ctor<int>();
}
But this isn't
struct wrapper
{
// Base class
struct Base
{
Base(){};
};
// declare factory function
template <class T> static Base* Ctor();
template <typename T> class C: public Base
{
friend Base* Ctor<T>();
private:
// Private constructor (can only make this from a friend)
C() {};
};
};
template <class T> wrapper::Base* wrapper::Ctor()
{
return new C<T>();
}
int main(int argc, char* argv[])
{
wrapper::Base* p = wrapper::Ctor<int>();
}
..... because you have to say
friend Base* wrapper::Ctor<T>();
No idea what Ctor it thought I meant - there's only one in the program!
Is this a funny in the MS compiler, or is it expected? If the latter,
is it worth an FAQ entry?
Andy