-1

我想通过使用 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。我在网上找到了一些其他示例,但尚未成功编译任何有用的东西。有什么建议么?

4

1 回答 1

2

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

删除该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;
...
于 2012-02-07T17:03:50.637 回答