在MSVC 2013上编译Boost MPL样本

时间:2014-12-29 13:35:32

标签: c++ boost boost-mpl msvc12

好吧,我成功构建了测试程序:

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

但是当我尝试使用boost :: mpl :: map时,它禁止了:

#include <boost/mpl/map.hpp>
#include <boost/mpl/assert.hpp>

int main()
{
    using std::is_same;
    using boost::mpl::at;
    using boost::mpl::long_;
    using boost::mpl::void_;
    using boost::mpl::map;
    using boost::mpl::pair;
    //using namespace boost::mpl;
    typedef map <
        pair<int, unsigned>
        , pair<char, unsigned char>
        , pair<long_<5>, char[17]>
        , pair < int[42], bool >
    > m;

    BOOST_MPL_ASSERT_RELATION(size<m>::value, == , 4);
    BOOST_MPL_ASSERT_NOT((empty<m>));

    BOOST_MPL_ASSERT((is_same< at<m, int>::type, unsigned >));
    BOOST_MPL_ASSERT((is_same< at<m, long_<5> >::type, char[17] >));
    BOOST_MPL_ASSERT((is_same< at<m, int[42]>::type, bool >));
    BOOST_MPL_ASSERT((is_same< at<m, long>::type, void_ >));

    return 0;
}

这是编译器输出:

1>------ Build started: Project: example, Configuration: Debug Win32 ------
1>  example.cpp
1>c:\projects\example\example\example.cpp(22): error C2027: use of undefined type 'boost::mpl::size<m>'
1>c:\projects\example\example\example.cpp(22): error C2065: 'value' : undeclared identifier
1>c:\projects\example\example\example.cpp(22): error C2975: 'x' : invalid template argument for 'boost::mpl::assert_relation', expected compile-time constant expression
1>          c:\boost_1_57_0\boost\mpl\assert.hpp(120) : see declaration of 'x'
1>c:\projects\example\example\example.cpp(22): error C2664: 'int boost::mpl::assertion_failed<0>(boost::mpl::assert<false>::type)' : cannot convert argument 1 from 'boost::mpl::failed ************boost::mpl::assert_relation<0,4,bool boost::mpl::operator ==(boost::mpl::failed,boost::mpl::failed)>::* ***********' to 'boost::mpl::assert<false>::type'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\boost_1_57_0\boost\mpl\assert.hpp(176): error C2027: use of undefined type 'boost::mpl::empty<m>'
1>          c:\projects\example\example\example.cpp(23) : see reference to class template instantiation 'boost::mpl::assert_arg_pred<boost::mpl::empty<m>>' being compiled
1>c:\boost_1_57_0\boost\mpl\assert.hpp(176): error C2146: syntax error : missing ';' before identifier 'p_type'
1>c:\boost_1_57_0\boost\mpl\assert.hpp(176): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost_1_57_0\boost\mpl\assert.hpp(177): error C2653: 'p_type' : is not a class or namespace name
1>c:\boost_1_57_0\boost\mpl\assert.hpp(177): error C2065: 'value' : undeclared identifier
1>c:\boost_1_57_0\boost\mpl\assert.hpp(177): error C2975: 'unnamed-parameter' : invalid template argument for 'boost::mpl::assert_arg_pred_impl', expected compile-time constant expression
1>          c:\boost_1_57_0\boost\mpl\assert.hpp(171) : see declaration of 'unnamed-parameter'
1>c:\boost_1_57_0\boost\mpl\assert.hpp(182): error C2027: use of undefined type 'boost::mpl::empty<m>'
1>          c:\projects\example\example\example.cpp(23) : see reference to class template instantiation 'boost::mpl::assert_arg_pred_not<boost::mpl::empty<m>>' being compiled
1>c:\boost_1_57_0\boost\mpl\assert.hpp(182): error C2146: syntax error : missing ';' before identifier 'p_type'
1>c:\boost_1_57_0\boost\mpl\assert.hpp(182): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\boost_1_57_0\boost\mpl\assert.hpp(183): error C2653: 'p_type' : is not a class or namespace name
1>c:\boost_1_57_0\boost\mpl\assert.hpp(183): error C2065: 'value' : undeclared identifier
1>c:\boost_1_57_0\boost\mpl\assert.hpp(183): error C2057: expected constant expression
1>c:\boost_1_57_0\boost\mpl\assert.hpp(184): error C2975: 'unnamed-parameter' : invalid template argument for 'boost::mpl::assert_arg_pred_impl', expected compile-time constant expression
1>          c:\boost_1_57_0\boost\mpl\assert.hpp(171) : see declaration of 'unnamed-parameter'
1>c:\projects\example\example\example.cpp(23): error C2668: 'boost::mpl::assert_not_arg' : ambiguous call to overloaded function
1>          c:\boost_1_57_0\boost\mpl\assert.hpp(203): could be 'boost::mpl::assert<false> boost::mpl::assert_not_arg<boost::mpl::empty<m>>(void (__cdecl *)(Pred),int)'
1>          with
1>          [
1>              Pred=boost::mpl::empty<m>
1>          ]
1>          c:\boost_1_57_0\boost\mpl\assert.hpp(194): or       'boost::mpl::failed ************boost::mpl::not_<boost::mpl::empty<m>>::* ***********boost::mpl::assert_not_arg<boost::mpl::empty<m>>(void (__cdecl *)(Pred),int)'
1>          with
1>          [
1>              Pred=boost::mpl::empty<m>
1>          ]
1>          while trying to match the argument list '(void (__cdecl *)(boost::mpl::empty<m>), int)'
1>c:\projects\example\example\example.cpp(25): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

即使执行以下操作:

#include <boost/mpl/map.hpp>

int main()
{
    using std::is_same;
    using boost::mpl::at;
    using boost::mpl::long_;
    using boost::mpl::map;
    using boost::mpl::pair;
    typedef map <
        pair<int, unsigned>
        , pair<char, unsigned char>
        , pair<long_<5>, char[17]>
        , pair < int[42], bool >
    > m;

    at<m, int>::type a;
    return 0;
}

失败:

1>------ Build started: Project: example, Configuration: Debug Win32 ------
1>  example.cpp
1>c:\projects\example\example\example.cpp(18): error C2027: use of undefined type 'boost::mpl::at<m,int>'
1>c:\projects\example\example\example.cpp(18): error C2065: 'type' : undeclared identifier
1>c:\projects\example\example\example.cpp(18): error C2146: syntax error : missing ';' before identifier 'a'
1>c:\projects\example\example\example.cpp(18): error C2065: 'a' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我已经安装了VS Community 2013 Update 4.我是否正确使用此版本,或者我安装的版本出现故障? Release notes for 1.57.0没有引用任何内容,也没有引用它们的bug系统。

1 个答案:

答案 0 :(得分:3)

基本上有一大堆缺少的标题。最后一个,最简单的,只是错过了

#include <boost/mpl/at.hpp>
#include <type_traits>

(那是在GCC上,因此MSVC可能需要更多,因为实现定义了间接头包含)。

完整的地图样本至少需要

#include <boost/mpl/assert.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/size.hpp>
#include <type_traits>

还有一些使用方法:

using boost::mpl::empty;
using boost::mpl::size;