Wednesday, May 1, 2019

c++ - Again on typename and template keywords

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::Pe a dependent name or not?



And what about Pa::Pe ?



And, after all, why this different behaviour of the two quoted compilers?



Can anyone clarify solving the puzzle?

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...