gpt4 book ai didi

c++ - 为 boost mpl 列表中的每种类型继承容器

转载 作者:搜寻专家 更新时间:2023-10-31 01:52:47 25 4
gpt4 key购买 nike

我有一个类,我想为 boost::mpl::vector 中的每个类从容器继承。换句话说,像这样:

template <typename types_vector>
class A : inherit from std::vector<type> for each type in types_vector {

};

例如,如果我有这个 vector :

typedef boost::mpl::vector<bool, int, double> types_vector_;

然后 A<types_vector_>将扩展为:

class A : public std::vector<bool>, public std::vector<int>, public std::vector<double> {

};

如何在不使用 C++11 功能的情况下执行此操作(其余代码尚未为此做好准备)?我认为使用 boost MPL 是可行的方法,尽管如果有 C++11 以外的替代方案,我可以考虑。

最佳答案

我认为这样的事情可以帮助你。

#include <boost/mpl/vector.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/size.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/greater_equal.hpp>
#include <vector>
#include <iostream>

namespace mpl = boost::mpl;

template<typename T,
typename = void>
struct Some
{
typedef std::vector<T> type;
};

template<typename T>
struct Some<T,
typename boost::enable_if_c
<
mpl::and_
<
mpl::is_sequence<T>,
mpl::greater_equal
<
mpl::size<T>,
mpl::int_<2>
>
>::type::value
>::type> :
public Some<typename mpl::front<T>::type>::type,
public Some<typename mpl::pop_front<T>::type>
{
};

template<typename T>
struct Some<T,
typename boost::enable_if_c
<
mpl::and_
<
mpl::is_sequence<T>,
mpl::equal_to
<
mpl::size<T>,
mpl::int_<1>
>
>::type::value
>::type> :
public Some<typename mpl::front<T>::type>::type
{
};

template<typename T>
struct Some<T,
typename boost::enable_if_c
<
mpl::and_
<
mpl::is_sequence<T>,
mpl::equal_to
<
mpl::size<T>,
mpl::int_<0>
>
>::type::value
>::type>
{
};


int main()
{
typedef mpl::vector<int, double> vect_t;
typedef Some<vect_t> vector;
vector vect;
vect.std::vector<int>::push_back(1);
vect.std::vector<double>::push_back(2);
std::cout << "int: " << vect.std::vector<int>::at(0) << std::endl;
std::cout << "double: " << vect.std::vector<double>::at(0) << std::endl;
}

http://liveworkspace.org/code/ec56ebd25b821c9c48a456477f0d42c9

关于c++ - 为 boost mpl 列表中的每种类型继承容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12116196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com