I have carefully read many answers concerning this topic, but nevertheless I cannot figure out EXACTLY when these two keywords ARE or AREN'T needed in the scope of a non-template function which is member of a nested template class.
My reference compilers are GNU g++ 4.9.2 and clang 3.5.0.
They behave hardly different on the following code where I put embedded
comments trying to explain what happens.
#include
// a simple template class with a public member template struct
template
class Pa
{
// anything
public:
template
struct Pe // a nested template
{
// anything
void f(const char *); // a non-template member function
};
template friend struct Pe;
};
// definition of the function f
template
template
void Pa :: Pe :: f(const char* c)
{
Pa p; // NO typename for both clang and GNU...
// the following line is ACCEPTED by both clang and GNU
// without both template and typename keywords
// However removing comments from typename only
// makes clang still accepting the code while GNU doesn't
// accept it anymore. The same happens if the comments of template
// ONLY are removed.
//
// Finally both compilers accept the line when both typename AND
// template are present...
/*typename*/ Pa::/*template*/ Pe q;
// in the following clang ACCEPTS typename, GNU doesn't:
/*typename*/ Pa::Pe qq;
// the following are accepted by both compilers
// no matter whether both typename AND template
// keywords are present OR commented out:
typename Pa::template Pe qqq;
typename Pa::template Pe qqqq;
std::cout << c << std::endl; // just to do something...
}
int main()
{
Pa::Pe pp;
pp.f("bye");
}
So, in the scope of f
is Pa
a dependent name or not?
And what about Pa
?
And, after all, why this different behaviour of the two quoted compilers?
Can anyone clarify solving the puzzle?
No comments:
Post a Comment