相关疑难解决方法(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万
查看次数

如何调用模板成员函数?

可能重复:
从模板函数调用的模板类的C++模板成员函数

template<class T1>
class A 
{
public:
    template<class T0>
    void foo() const {}
};

template<class T0,class T1>
void bar( const A<T1>& b )
{
    b.foo<T0>();  // This throws " expected primary-expression before ‘>’ token"
}
Run Code Online (Sandbox Code Playgroud)

我可以改成它

b->A<T1>::template foo<T0>();
Run Code Online (Sandbox Code Playgroud)

编译好.不过我也可以改成它

b.A<T1>::template foo<T0>();
Run Code Online (Sandbox Code Playgroud)

编译也很好.是吗?

如何在原始代码的意义上正确调用模板成员函数?

c++ templates

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

如何在C++模板代码中修复'预期的'primary-expression'错误?

这是另一个VC9与GCC 4.2编译错误问题.以下代码使用VC9(Microsoft Visual C++ 2008 SP1)编译,但不适用于Mac上的GCC 4.2:

struct C
{
    template< typename T >
    static bool big() { return sizeof( T ) > 8; }
};

template< typename X >
struct UseBig
{
    static bool test()
    {
        return X::big< char >(); // ERROR: expected primary-expression
    }                            // before 'char'
};

int main()
{
    C::big< char >();
    UseBig< C >::test();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我有什么想法可以解决这个问题?

c++ gcc templates

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

为什么这个小C++程序不能用G ++编译?

以下代码不能使用G ++ 4.5或4.6(快照)进行编译.它将使用Digital Mars Compiler 8.42n进行编译.

template <int I>
struct Foo {
  template <int J>
  void bar(int x) {}
};

template <int I>
void test()
{
  Foo<I> a;
  a.bar<8>(9);
};

int main(int argc, char *argv[]) {
  test<0>();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误消息是:

bugbody.cpp: In function 'void test() [with int I = 0]':
bugbody.cpp:16:11:   instantiated from here
bugbody.cpp:11:3: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
Run Code Online (Sandbox Code Playgroud)

程序是否有效C++?

c++ gcc templates g++

14
推荐指数
1
解决办法
1542
查看次数

无法引用嵌套在模板参数中的模板名称

我有以下代码:

template <typename Provider>
inline void use()
{
    typedef Provider::Data<int> D;
}
Run Code Online (Sandbox Code Playgroud)

我基本上试图使用一些'Provider'类的模板类成员'Data',应用于'int',但是我得到以下错误:

util.cpp:5: error: expected init-declarator before '<' token
util.cpp:5: error: expected `,' or `;' before '<' token
Run Code Online (Sandbox Code Playgroud)

我在Solaris系统上使用GCC 4.3.3.

c++

8
推荐指数
2
解决办法
376
查看次数

模板化类的模板化成员的编译失败.VC++有效,但G ++失败了

以下代码在Visual C++ 2010下编译良好,但在Android NDK r8b下不在GCC 4.6下编译.

template<typename A>
struct foo
{
    template<typename B>
    B method()
    {
        return B();
    }
};

template<typename A>
struct bar
{
    bar()
    {
        f_.method<int>(); // error here
    }

private:
    foo<A> f_;
};
Run Code Online (Sandbox Code Playgroud)

GCC给出的错误是

error : expected primary-expression before 'int'
error : expected ';' before 'int'
Run Code Online (Sandbox Code Playgroud)

对于标记线.对于我的生活,我无法弄清楚什么是错的.

c++

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

标签 统计

c++ ×6

templates ×4

gcc ×2

c++-faq ×1

dependent-name ×1

g++ ×1

typename ×1