在模板,在那里,为什么我必须把typename和template上依赖的名字呢?究竟什么是依赖名称?我有以下代码:
template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
// ...
template<typename U> struct inUnion {
// Q: where to add typename/template here?
typedef Tail::inUnion<U> dummy;
};
template< > struct inUnion<T> {
};
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
// ...
template<typename U> struct inUnion {
char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …Run Code Online (Sandbox Code Playgroud) 我遇到以下形式的代码问题:
template<class Type>
class Class1 {
public:
template<class TypeName1> TypeName1* method1() const {return 0;}
};
struct Type1{};
struct Type2{};
class Class2 {
public:
template<typename TypeName1, typename TypeName2>
int method2() {
Class1<TypeName2> c;
c.method1<TypeName1>();
return 0;
}
int method1() {
return method2<Type1, Type2>();
}
};
int
main() {
Class2 c;
return c.method1();
}
Run Code Online (Sandbox Code Playgroud)
在codepad上使用编译器编译时:
我收到以下错误:
t.cpp:在成员函数'int Class2 :: method2()'中:第15行:错误:由于-Wfatal-errors,'>'令牌编译终止之前的预期primary-expression.
违规行是模板成员函数的调用:
c.method1<TypeName1>();
Run Code Online (Sandbox Code Playgroud)