2012-02-07 108 views
0

我想通過使用boost :: mpl :: fold來處理一些模板參數。目前,我仍然堅持Boost提供的樣本,即使這對我不起作用。我得到以下錯誤:C++ Boost :: MPL fold示例 - 錯誤的參數個數

..\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。我在網上發現了一些其他的例子,但在編譯任何有用的東西方面尚未成功。有什麼建議麼?

+1

你的例子似乎是好的:http://ideone.com/XUom2(注意C的版本++我聯繫的是舊標準 - 而不是11) – Nim 2012-02-07 17:06:05

回答

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; 
... 
+0

好,無論是性病和提升一直是一個壞主意。沒有想到這一點,非常感謝! – user1034081 2012-02-07 17:11:20