相关疑难解决方法(0)

麻烦在宏中使用模板参数

我正在尝试编译下面的一段代码,我在专门用于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)

链接:http://codepad.org/qIiKsw4l

c++ parameters macros templates

22
推荐指数
4
解决办法
1万
查看次数

将需要逗号的模板传递给单参数宏

我有一些代码基本上可以浓缩为

#define FOO(a)
FOO(std::map<int, int>);
Run Code Online (Sandbox Code Playgroud)

但它会发出编译错误(宏的实际参数太多FOO)。

显然,预处理器认为我已经提供了std::map<intint>作为参数。

有没有办法解决这个问题?预处理器不会以这种方式处理带逗号的带引号的字符串。

c++ c-preprocessor

6
推荐指数
1
解决办法
1084
查看次数

使用boost foreach和自己模板的项目

我有一个std::deque< std::pair<int, int> >我想迭代使用BOOST_FOREACH.

我尝试了以下方法:

  #define foreach_ BOOST_FOREACH

  // declaration of the std::deque
  std::deque< std::pair<int, int> > chosen;

  foreach_( std::pair<int,int> p, chosen )
  {
     ... 
  }
Run Code Online (Sandbox Code Playgroud)

但是当我编译它(在Visual Studio中)时,我收到以下错误:

warning C4002: too many actual parameters for macro 'BOOST_FOREACH'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ')' before '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : ')'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ';' before '{'
1>c:\users\beeband\tests.cpp(133): error C2181: illegal else …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-foreach

3
推荐指数
2
解决办法
386
查看次数