在模板,在那里,为什么我必须把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 <typename T, typename K> class A {
public:
T t;
K k;
template <int i, int unused = 0> struct AttributeType {
};
template <int i> AttributeType<i> getAttr();
};
template <typename T, typename K> template <int i> A<T, K>::AttributeType<i> A<T, K>::getAttr<i>() {
return t;
}
Run Code Online (Sandbox Code Playgroud)
我无法提出正确的语法来定义实现A::getAttr().当前代码无法在getAttr定义行编译:
error: function template partial specialization ‘getAttr<i>’ is not allowed
我该如何重新定义函数定义?