在模板,在那里,为什么我必须把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) 可能重复:
从模板函数调用的模板类的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)
编译也很好.是吗?
如何在原始代码的意义上正确调用模板成员函数?
这是另一个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)
我有什么想法可以解决这个问题?
以下代码不能使用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++?
我有以下代码:
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.
以下代码在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)
对于标记线.对于我的生活,我无法弄清楚什么是错的.