相关疑难解决方法(0)

我必须在何处以及为何要使用"模板"和"typename"关键字?

在模板,在那里,为什么我必须把typenametemplate上依赖的名字呢?究竟什么是依赖名称?我有以下代码:

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)

c++ templates c++-faq dependent-name typename

1061
推荐指数
8
解决办法
15万
查看次数

令人困惑的模板错误

我一直玩clang一段时间,我偶然发现了"test/SemaTemplate/dependent-template-recover.cpp"(在clang发行版中),它应该提供从模板错误中恢复的提示.

整个过程可以很容易地删除到最小的例子:

template<typename T, typename U, int N> struct X {
    void f(T* t)
    {
        // expected-error{{use 'template' keyword to treat 'f0' as a dependent template name}}
        t->f0<U>();
    }
};
Run Code Online (Sandbox Code Playgroud)

clang产生的错误消息:

tpl.cpp:6:13: error: use 'template' keyword to treat 'f0' as a dependent template name
         t->f0<U>();
            ^
            template 
1 error generated.
Run Code Online (Sandbox Code Playgroud)

...但我很难理解究竟应该插入template关键字以使代码在语法上正确?

c++ templates llvm clang

78
推荐指数
4
解决办法
2万
查看次数

c ++编译器将模板语法视为"<"运算符

我正在尝试编译以下代码:

struct A {
    template<int N> static void a() {}
};

template<> void A::a<5>() {}

template<class T>
struct B {
    static void b() {
        T::a<5>();
    }
};

void test() {
    A::a<5>();
    B<A>::b();
}
Run Code Online (Sandbox Code Playgroud)

和编译器解释<T::a<5>作为操作者<,从而导致错误:

invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
Run Code Online (Sandbox Code Playgroud)

有没有办法在T::a<5>没有编译器错误的情况下显式实例化?谢谢.

gcc版本4.5.1 20100924(Red Hat 4.5.1-4)(GCC)

c++ templates compiler-errors

3
推荐指数
1
解决办法
164
查看次数

模板模板成员的消歧模板关键字:何时完全?

关于模板消歧器的问题在这里给出:

模板消歧器

在答案中我们可以读到:

ISO C++ 03 14.2/4

当成员模板专业化的名称出现之后.或 - >在postfix-expression中,或在qualified-id中的nested-name-specifier之后,postfix-expression或qualified-id显式依赖于template-parameter(14.6.2),成员模板名称必须是以关键字模板为前缀.否则,假定该名称命名非模板.

现在我来到这个我不太明白的例子:

template <class T>
class Base {
  public:


  template <int v>
    static int baseGet() {return v;}

  class InnerA {
    public:

    template <int v>
      static int aget() {return v;}

  };

  class InnerB {
    public:
      typedef Base BaseType;
      typedef BaseType::InnerA OtherType;

      template <int v>
        static int baseGet() {return BaseType::baseGet<v>();} //(A)

      template <int v>
        static int aget() {return OtherType::aget<v>();} //(B)
  };
};
Run Code Online (Sandbox Code Playgroud)

它显然无法编译.你需要template在(B)行:OtherType::template aget<v>();.然而,g ++(4.4.3)和clang ++(2.9)都没有抱怨template线(A)缺乏.为什么?BaseType …

c++ templates

2
推荐指数
1
解决办法
854
查看次数