我已经仔细阅读了许多关于这个主题的答案,但是我无法确切地知道这两个关键字何时在嵌套模板类的成员的非模板函数的范围内是或不需要的。
我的参考编译器是 GNU g++ 4.9.2 和 clang 3.5.0。
它们在以下代码中的行为几乎没有什么不同,我在其中放置了嵌入式注释以试图解释发生了什么。
#include <iostream>
// a simple template class with a public member template struct
template <class Z>
class Pa
{
// anything
public:
template <class U>
struct Pe // a nested template
{
// anything
void f(const char *); // a non-template member function
};
template <class U> friend struct Pe;
};
// definition of the function f
template <class AAA>
template <class BBB>
void Pa<AAA> :: Pe<BBB> :: f(const char* c)
{
Pa<AAA> 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<AAA>::/*template*/ Pe<BBB> q;
// in the following clang ACCEPTS typename, GNU doesn't:
/*typename*/ Pa<AAA>::Pe<int> qq;
// the following are accepted by both compilers
// no matter whether both typename AND template
// keywords are present OR commented out:
typename Pa<int>::template Pe<double> qqq;
typename Pa<double>::template Pe<BBB> qqqq;
std::cout << c << std::endl; // just to do something...
}
int main()
{
Pa<char>::Pe<int> pp;
pp.f("bye");
}
那么,在范围内f
是Pa<double>::Pe<BBB>
不是依赖名呢?
那么Pa<AAA>::Pe<int>
呢?
毕竟,为什么这两个引用的编译器会有这种不同的行为呢?
任何人都可以澄清解决这个难题吗?