【问题标题】:How do I merge two mpl maps producing a new map?如何合并两个 mpl 地图以生成新地图?
【发布时间】:2017-01-12 14:59:38
【问题描述】:

但是,我对以下代码进行了修改,但我似乎遗漏了一点。它不会编译。我有两张地图 int -> int。我想生成第三个 int -> int 映射,其中包含两个原件中的所有键值对。 (VS2013) 有人吗?

#include <boost/mpl/map.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/has_key.hpp>

typedef boost::mpl::map <
    boost::mpl::pair < boost::mpl::int_<7>, boost::mpl::int_<59>  >
>::type Original1;

typedef boost::mpl::map <
    boost::mpl::pair < boost::mpl::int_<11>, boost::mpl::int_<61>  >
>::type Original2;

typedef boost::mpl::copy <
    Original1,
    boost::mpl::back_inserter < Original2 >
>::type Merged;

BOOST_MPL_ASSERT((boost::mpl::has_key<Merged, 7>));

int _tmain(int argc, _TCHAR* argv[])
{

    const int i = boost::mpl::at<Merged, boost::mpl::int_<7> >::type::value;

    return 0;
}

【问题讨论】:

    标签: c++ c++11 template-meta-programming boost-mpl


    【解决方案1】:

    您的代码有两个问题。简单的在这里:

    BOOST_MPL_ASSERT((boost::mpl::has_key<Merged, 7>));
    

    键是类型,7 不是类型。您要检查密钥mpl::int_&lt;7&gt;

    第二个在这里:

    typedef boost::mpl::copy <
        Original1,
        boost::mpl::back_inserter < Original2 > // <==
    >::type Merged;
    

    mpl::back_inserterstd::back_inserter 的元编程等价物,它创建了一个通过push_back() 输出的OutputIterator。同样,back_inserter 需要“后向可扩展序列”,因为它使用 mpl::push_backmpl::map 不是向后可扩展序列。如果您查看它的reference,则没有push_back,只有insert。所以你需要这样做:

    using Merged =
        mpl::copy<
            Original1,
            mpl::inserter<Original2, mpl::insert<mpl::_1, mpl::_2>> // <==
        >::type;
    

    我不太明白 mpl::quote 在做什么,但它似乎坏了(呃,实际上,mpl::insert 需要 3 个参数,mpl::quote3 不允许默认最后一个参数)。如果你写:

    template <template <class... > class F>
    struct quote {
        template <class... Args>
        struct apply {
            using type = typename F<Args...>::type;
        };
    };
    

    那么你可以写:

    mpl::inserter<Original2, quote<mpl::insert>>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-06
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多