我正在尝试为程序提供一种向库中的变体添加新对象的方法,但我遇到了一些神秘的错误。
#include <boost/mpl/copy.hpp>
#include <boost/mpl/joint_view.hpp>
#include <boost/mpl/list.hpp>
#include <boost/variant/variant.hpp>
struct InternalType1 {};
struct InternalType2 {};
template <typename LocalTypes>
struct Foo
{
typedef boost::mpl::list<
InternalType1,
InternalType2
> Types;
typename boost::make_variant_over<
typename boost::mpl::joint_view<
Types,
LocalTypes
>::type
>::type container_;
// typename boost::make_variant_over<
// typename boost::mpl::copy<
// LocalTypes,
// boost::mpl::back_inserter<Types>
// >::type
// >::type container_;
};
struct LocalType1 {};
struct LocalType2 {};
int main()
{
typedef boost::mpl::list<
LocalType1,
LocalType2
> Types;
Foo<Types> foo;
}
通过使用 mpl::joint_view
(我认为这是实现此目的的最有效方法),我收到以下错误:
/usr/local/include/boost/mpl/clear.hpp:29:7: error: implicit instantiation of undefined template
通过取消对其他尝试的注释,使用 mpl::copy
,并将其替换为原来的,然后错误更改:
/usr/local/include/boost/mpl/aux_/push_back_impl.hpp:40:9: error: no matching function for call to 'assertion_failed'
有趣的是,它有 following comment :
// should be instantiated only in the context of 'has_push_back_impl';
// if you've got an assert here, you are requesting a 'push_back'
// specialization that doesn't exist.
这些错误对我来说都没有任何意义,因为第一个错误,我看不到哪些模板不完整,第二个错误,我没有使用哪个 push_back 特化?
最佳答案
问题是 boost::mpl::clear<>
未针对 joint_view
实现...因此巨大的编译器转储终止于:
/usr/local/include/boost/mpl/clear.hpp:29:7: error: implicit instantiation of undefined template 'boost::mpl::clear_impl<boost::mpl::aux::joint_view_tag>::apply<boost::mpl::joint_view<boost::mpl::list<InternalType1, InternalType2, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::mpl::list<LocalType1, LocalType2, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na> > >'
(我不知道如何正确格式化)
这可能只是库中的一个疏忽,或者可能只是不清楚在这种情况下应该返回哪个空序列类型。如果你想使用 joint_view
,您必须提供 clear_impl
的特化某处:
namespace boost { namespace mpl {
template <>
struct clear_impl<aux::joint_view_tag>
{
template <typename JV>
struct apply {
typedef list<> type; // since you're using list I figured
// I would too.
};
};
} }
这样,您的代码就可以在 gcc 和 clang 上为我编译。
或者,如果将内容添加到 namespace boost::mpl
让你觉得有点阴暗,但你仍然想坚持使用 list
s,你可以只使用 insert_range
:
typename boost::make_variant_over<
typename boost::mpl::insert_range<
Types,
typename boost::mpl::end<Types>::type,
LocalTypes
>::type
>::type container_;
关于c++ - 使用 MPL 列表扩展 boost 变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581311/