[英]Extending boost variant with an MPL list
我正在嘗試為程序提供一種向庫中的變體添加新對象的方法,但是遇到一些隱秘的錯誤。
#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'
有趣的是,它具有以下評論 :
// 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專業化?
問題是沒有為joint_view
實現boost::mpl::clear<>
...因此,巨大的編譯器轉儲終止於:
/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> > >'
(我不知道如何正確格式化)
這可能只是庫中的一個疏忽,或者可能不清楚在這種情況下應返回哪種空Sequence類型。 如果要使用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
insert_range
您insert_range
有點陰暗,但您仍然想堅持使用list
,則可以使用insert_range
:
typename boost::make_variant_over<
typename boost::mpl::insert_range<
Types,
typename boost::mpl::end<Types>::type,
LocalTypes
>::type
>::type container_;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.