我正在尝试编译下面的一段代码,我在专门用于std :: vector的行上得到一个错误,似乎传入的一个参数在某种程度上被假定为两个参数.它可能与角括号有关吗?
是否有一种特殊的方式/机制,通过这些参数可以正确地传递给宏?
#include <vector>
template<typename A>
struct AClass {};
#define specialize_AClass(X)\
template<> struct AClass<X> { X a; };
specialize_AClass(int) //ok
specialize_AClass(std::vector<int,std::allocator<int> >) //error
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误如下:
1 Line 55: error: macro "specialize_AClass" passed 2 arguments, but takes just 1
2 Line 15: error: expected constructor, destructor, or type conversion before 'int'
3 compilation terminated due to -Wfatal-errors.
Run Code Online (Sandbox Code Playgroud)
我遇到了一个qt foreach和一个带有多个模板参数的模板的问题.
QVector<Node<T, U> > nodes;
...
[append some data]
...
foreach(const Node<T, U>& node, nodes) {
...
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
error: use of undeclared identifier 'Q_FOREACH'
Run Code Online (Sandbox Code Playgroud)
我想这是由于,模板中的原因,因为Qt宏没有在另一个模板声明中检测到它.如何在不使用普通for循环或C++ 11的情况下解决这个问题?
我有一个宏,它接受一些参数,其中一个是一个类型.例如:
#define macro(T,x) T x
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用模板类型对其进行实例化,请说:
macro(Type<int,float>,var);
Run Code Online (Sandbox Code Playgroud)
这不会按预期工作,因为预处理器不知道任何模板.它会将其解析为三个参数:
macro(
Type<int,
float>,
var
);
Run Code Online (Sandbox Code Playgroud)
有没有解决的办法?我用了一个,typedef Type<int,float> T;但我想知道是否可以在不引入另一种(可能是全局的)类型的情况下进行内联.
也许逗号可以守卫?