【问题标题】:C++ Boost::MPL fold example - wrong number of argumentsC++ Boost::MPL 折叠示例 - 参数数量错误
【发布时间】:2012-02-07 16:55:15
【问题描述】:

我想通过使用 boost::mpl::fold 来处理一些模板参数。目前,我仍然坚持使用 Boost 提供的示例,因为即使这对我也不起作用。我收到以下错误:

..\src\main.cpp:18:32: error: template argument 2 is invalid
..\src\main.cpp:18:37: error: wrong number of template arguments (4, should be 3)

以下代码取自http://www.boost.org/doc/libs/1_48_0/libs/mpl/doc/refmanual/fold.html

#include <string>
#include <iostream>

#include <boost/mpl/fold.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/type_traits.hpp>

using namespace std;
using namespace boost;
using namespace boost::mpl;
using namespace boost::type_traits;

typedef vector<long,float,short,double,float,long,long double> types;
typedef fold<
      types
    , int_<0>
    , if_< is_float<_2>,next<_1>,_1 >
    >::type number_of_floats;

BOOST_MPL_ASSERT_RELATION( number_of_floats::value, ==, 4 );

int main(){
}

我正在使用标志“-std=c++11”运行 mingw 4.7.0。我在网上找到了一些其他示例,但尚未成功编译任何有用的东西。有什么建议吗?

【问题讨论】:

  • 您的示例似乎没问题:ideone.com/XUom2(请注意我链接的 C++ 版本是旧标准 - 不是 11)

标签: c++ templates boost metaprogramming boost-mpl


【解决方案1】:

你搞乱了命名空间。使很多符号模棱两可。

删除using,该示例对我来说很好。

...
using namespace boost;

typedef mpl::vector<long,float,short,double,float,long,long double> types;
typedef mpl::fold<
    types
    , mpl::int_<0>
    , mpl::if_< is_float<boost::mpl::_2>,boost::mpl::next<boost::mpl::_1>,boost::mpl::_1 >
>::type number_of_floats;
...

【讨论】:

  • 好吧,std 和 boost 都是个坏主意。没想到,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
相关资源
最近更新 更多