【发布时间】:2014-12-20 14:48:57
【问题描述】:
我正在尝试为程序提供一种将新对象添加到库中的变体的方法,但我遇到了一些神秘的错误。
#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 专业化?
【问题讨论】:
-
第二个很容易理解,
mpl::list不能和push_back一起使用。您可以使用boost::mpl::front_inserter<Types>或在Typestypedef 中使用mpl::vector。 -
啊啊,就是这样。谢谢!由于joint_view 解决方案可能更好,或者两者在生产中做的更好,我会留下这个问题。
-
除非您打算修改
make_variant_over,否则我看不到更好的方法。mpl::copyfrommpl::vectors 是你问我的方式 -
那么
joint_view在这里不可行吗?它在make_variant_over的文档中说它需要一个 MPL 序列。然后joint_view文档声明joint_view 是一个前向序列。所以这两个应该是兼容的吧?
标签: c++ boost metaprogramming boost-mpl