我試圖找到一種解決方法來使用C++ 03中的可變參數模板。C++ 03:使用boost的可變參數模板
我想實現的是 - 在模板類中 - 實例化一個boost::tuple
屬性,該屬性由每個單個類模板參數的向量組成。
這是它會是什麼樣子用C++ 11個可變參數模板:
template<typename ...Parameters>
class Foo
{
typedef std::tuple<std::vector<Parameters>...> Vectors_t;
Vectors_t _vectorsTuple;
}
我想達到同樣的使用boost ::元組和boost :: MPL或任何其他可能的方式。
感謝
UPDATE
這是我想出了一個最終的解決方案。 部分基於@stefanmoosbrugger命題。
template<class TypesList>
class FooImpl
{
typedef TypesList TypesList_t;
typedef typename boost::mpl::transform <
TypesList_t,
std::vector<boost::mpl::_1>
>::type VectorTypesList_t;
typedef typename boost::mpl::reverse_fold<
VectorTypesList_t,
boost::tuples::null_type,
boost::tuples::cons<boost::mpl::_2, boost::mpl::_1>
>::type VectorsTuple_t;
protected:
VectorsTuple_t _vectorsTuple;
};
template<class Type1,
class Type2 = boost::mpl::na,
class Type3 = boost::mpl::na,
class Type4 = boost::mpl::na> /* Made it up to four possible types as an example */
class Foo : public FooImpl<boost::mpl::vector<Type1, Type2, Type3, Type4> >{};
謝謝,這不是太糟糕:) 從mpl :: vector到實際元組的變換可能不那麼難看嗎? 我們真的需要BOOST_PP_ENUM宏和自定義push_front嗎? 想知道如果可以使用mpl :: transform而不是摺疊 – codeJack
以更平滑的方式實現,則可以使用'mpl_vec_t'上的'mpl :: transform'將元素從'Tx'轉換爲'std :: vector '然後創建一個boost融合向量('boost :: fusion :: result_of :: as_vector :: type').. :)肯定看起來不那麼難看。 –
您能否使用此解決方案更新您的代碼? :) – codeJack