使用宏中的模板参数的问题 [英] Trouble with template parameters used in macros
问题描述
我试图编译下面的代码,我得到一个专门的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;
}
我得到的错误如下:
1行55:错误:宏specialize_AClass传递2个参数,但只需1
行15:错误:expected constructor,destructor,或'int'之前的类型转换
3由于-Wfatal-errors而导致编译终止。
链接: http: /codepad.org/qIiKsw4l
您有两个选项。其中一个已经提到:使用 __ VA_ARGS __
。然而这有一个缺点,它不工作在严格的C ++ 03,但需要一个足够的C99 / C ++ 0x兼容的预处理器。
另一个选项是对类型名称加括号。但不像另一个答案声称,它不像简单的括号类型名称一样简单。编写如下的专业化是错误的
//错误,无效!
模板<> struct AClass<(int)> {Xa; };
我已经解决了这个问题(并且可能使用相同的底层)传递类型名称
template< typename T> struct get_first_param;
template< typename R,typename P1> struct get_first_param< R(P1)> {
typedef P1 type;
};
这样, get_first_param< void(X)> :: type
表示类型 X
。现在您可以将宏重写为
#define specialize_AClass(X)\
template< struct AClass< get_first_param< void X> :: type> {
get_first_param< void X> :: type a;
};
你只需要传递括号中的类型。
I'm trying to compile the following piece of code, I get an error on the line which specializes std::vector, it seems the one parameter being passed-in is somehow being assumed to be two parameters. Is it perhaps something to do with angle-brackets?
Is there a special way/mechanism where by such parameters can be correctly passed to the macro?
#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;
}
The error that I get is as follows:
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.
Link: http://codepad.org/qIiKsw4l
You have two options. One of which was mentioned already: Using __VA_ARGS__
. This however has the disadvantage that it doesn't work in strict C++03, but requires a sufficiently C99/C++0x compatible preprocessor.
The other option is to parenthesize the type-name. But unlike another answer claims, it's not as easy as just parenthesizing the type name. Writing a specialization as follows is ill-formed
// error, NOT valid!
template<> struct AClass<(int)> { X a; };
I have worked around this (and boost probably uses the same under the hood) by passing the type name in parentheses, and then building up a function type out of it
template<typename T> struct get_first_param;
template<typename R, typename P1> struct get_first_param<R(P1)> {
typedef P1 type;
};
With that, get_first_param<void(X)>::type
denotes the type X
. Now you can rewrite your macro to
#define specialize_AClass(X) \
template<> struct AClass<get_first_param<void X>::type> {
get_first_param<void X>::type a;
};
And you just need to pass the type wrapped in parentheses.
这篇关于使用宏中的模板参数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!