【问题标题】:The usage of boost::mpl::bindboost::mpl::bind 的使用
【发布时间】:2014-04-14 23:26:36
【问题描述】:

当我使用以下测试代码尝试 mpl::bind 函数时,我未能通过 gcc 中的编译器, 谁能帮我找出问题,非常感谢。

#include <iostream>
#include <typeinfo>
#include <string>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/char.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/arg.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/add_pointer.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/quote.hpp>

using namespace std;

using namespace boost::mpl;

template< typename T1,typename T2 >
struct int_plus:boost::mpl::int_< (T1::value+T2::value) >
{
};

int main()
{
    typedef boost::mpl::lambda< int_plus<_1, _2 > >::type test1;        //-fine

    // test2 define is causeing error
    typedef boost::mpl::bind  < int_plus<_1, _2 > > test2;              //-error?

    typedef boost::mpl::lambda< quote2<int_plus>, _2, _1 >::type test3; //-fine
    typedef boost::mpl::bind< quote2<int_plus>, _2, _1 > test4;         //-fine
    typedef test1::apply<int_<42>, int_<23>>::type test5;               //-fine
    typedef test2::apply<int_<42>, int_<23>>::type test6;               //-error
    typedef test3::apply<int_<42>, int_<24>>::type test7;               //-fine
    typedef test4::apply<int_<42>, int_<24>>::type test8;               //-fine
    BOOST_MPL_ASSERT_RELATION( test5::value, ==, 65 );                  //-fine
    //BOOST_MPL_ASSERT_RELATION( test6::value, ==, 65 );
}

错误信息:

||=== 构建:在 jtest2 中调试(编译器:GNU GCC 编译器)===|

C:\boost\mpl\aux_\preprocessed\gcc\apply_wrap.hpp||在'struct boost::mpl::apply_wrap0, mpl_::arg, mpl_::bool_ >'的实例化中: |

C:\boost\mpl\aux_\preprocessed\gcc\bind.hpp|86|需要来自 'struct boost::mpl::bind0, mpl_::arg > >::apply, mpl_:: int_ >'| C:\ls\jtest2\main.cpp|30|从这里需要|

C:\boost\mpl\aux_\preprocessed\gcc\apply_wrap.hpp|20|错误:'struct int_plus, mpl_::arg >'中没有​​名为'apply'的类模板|

C:\boost\mpl\aux_\preprocessed\gcc\bind.hpp||在实例化'struct boost::mpl::bind0, mpl_::arg >::apply, mpl_:: int_ >':|

C:\ls\jtest2\main.cpp|30|从这里需要| C:\boost\mpl\aux_\preprocessed\gcc\bind.hpp|86|错误:在 'struct boost::mpl::apply_wrap0、mpl_::arg >、mpl_:: 中没有名为 'type' 的类型bool_ >'|

||=== 构建失败:2 个错误,5 个警告(0 分钟,0 秒)===|

【问题讨论】:

  • 你能发布完整的错误信息吗?您所显示的只是有关编译器在遇到错误时实例化模板的位置的信息。
  • test4 不能解决您遇到的test2 的问题吗?
  • 是的,test4 工作正常,test1、test3 都工作。我无法理解 test2,也许我偷了更多关于 mpl::bind 的检查
  • 我已更新问题以包含完整的测试代码和错误消息。

标签: c++ boost boost-mpl


【解决方案1】:

在检查bind的定义和语义后,它需要一个元函数类作为第一个参数,即元函数不能工作;

我们有几种方法可以将元函数转换为元函数类,在这个例子中 元函数 int_plus 可以被

隐藏

1) quote2(int_plus)

2) int_plus_f

3) int_plus_f2

#include <iostream>
#include <typeinfo>
#include <string>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/char.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/arg.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/add_pointer.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/quote.hpp>

using namespace std;
using namespace boost::mpl;

template< typename T1,typename T2 >
struct int_plus:boost::mpl::int_< (T1::value+T2::value) >
{
};

struct int_plus_f  // method 1 to get metafunction class, not perfect for lambda
{
    template< typename T1,typename T2 >
    struct apply:boost::mpl::int_< (T1::value+T2::value) >
    {
    };
};

struct int_plus_f2 // method 2 to get metafunction class, perfect for lambda
{
    template< typename A1, typename A2 > struct apply
            : int_plus<A1,A2>
    {
    };
};

int main()
{
    //bind define:
    //    typedef bind<f,a1,...an> g;
    //bind parameters:
    //    F Metafunction Class An metafunction class to perform binding on.
    //    A1,... An Any type Arguments to bind.

    //lambda define:
    //    typedef lambda<x>::type f;
    //    typedef lambda<x,Tag>::type f;
    //lambda parameters
    //    X Any type An expression to transform.
    //    Tag Any type A tag determining transform semantics
    //lambda Semantics equivalent to
    //    typedef protect< bind< quoten<X> , lambda<a1>::type,... lambda<an>::type > > f;
    //quote define:
    //    typedef quoten<f> g;
    //    typedef quoten<f,tag> g;
    //quote2 Semantics Equivalent to
    //    struct g{
    //        template< typename A1,typename A2 >
    //            struct apply : f<A1,A2>{};
    //            };




    typedef boost::mpl::lambda< int_plus<_1, _2 > >::type test1;        //-fine
    typedef boost::mpl::bind  < int_plus_f,_1, _2  > test2;             //-fine
    typedef boost::mpl::bind  < int_plus_f2,_1, _2  > test3;            //-fine
    typedef boost::mpl::lambda< int_plus_f2,_1, _2  >::type test4;      //-fine
    typedef boost::mpl::lambda< quote2<int_plus>, _2, _1 >::type test5; //-fine
    typedef boost::mpl::bind< quote2<int_plus>, _2, _1 > test6;         //-fine
    typedef test1::apply<int_<42>, int_<22>>::type result1;             //-fine
    typedef test2::apply<int_<42>, int_<23>>::type result2;             //-fine
    typedef test3::apply<int_<42>, int_<24>>::type result3;             //-fine
    typedef test4::apply<int_<42>, int_<25>>::type result4;             //-fine
    typedef test5::apply<int_<42>, int_<26>>::type result5;             //-fine
    typedef test6::apply<int_<42>, int_<27>>::type result6;             //-fine
    BOOST_MPL_ASSERT_RELATION( result1::value, ==, 64 );                //-fine
    BOOST_MPL_ASSERT_RELATION( result2::value, ==, 65 );                //-fine
    BOOST_MPL_ASSERT_RELATION( result3::value, ==, 66 );                //-fine
    BOOST_MPL_ASSERT_RELATION( result4::value, ==, 67 );                //-fine
    BOOST_MPL_ASSERT_RELATION( result5::value, ==, 68 );                //-fine
    BOOST_MPL_ASSERT_RELATION( result6::value, ==, 69 );                //-fine


    //apply :  Invokes a Metafunction Class or a Lambda Expression F with arguments A1,... An.
    //    typedef apply<f,a1,...an>::type t;
    //apply parameters
    //    F Lambda Expression: An expression(e.g.: a metafunction) to invoke,
    //      metafunction class is fine also
    //    A1,... An Any type Invocation arguments.
    // apply  Semantics Equivalent to
    //    typedef apply_wrapn< lambda<f>::type,a1,... an>::type t;.

    typedef apply< int_plus<_1,_2>, int_<2>, int_<3> >::type r1;
    typedef apply< quote2<int_plus>, int_<2>, int_<3> >::type r2;
    typedef apply< int_plus_f, int_<2>, int_<3> >::type r3;
    typedef apply< int_plus_f2, int_<2>, int_<3> >::type r4;

    BOOST_MPL_ASSERT_RELATION( r1::value, ==, 5 );
    BOOST_MPL_ASSERT_RELATION( r2::value, ==, 5 );
    BOOST_MPL_ASSERT_RELATION( r3::value, ==, 5 );
    BOOST_MPL_ASSERT_RELATION( r4::value, ==, 5 );

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    相关资源
    最近更新 更多