更改给定STL容器的value_type

时间:2011-02-19 17:07:31

标签: c++ templates stl metaprogramming containers

假设我有一个STL容器类型(不是对象),比如vector<A>。现在它是value_type A,所以我想将其更改为B

基本上,我想要一个这种形式的类模板,或者它的变体:

template<typename container, typename new_value_type>
struct change_value_type
{
    typedef /*....*/  new_container;
};

这样我就可以通过以下方式使用它:

typename change_value_type<vector<A>, B>::new_container  vectorOfB; 
vectorOfB.push_back(B());
vectorOfB.push_back(B());
vectorOfB.push_back(B());
//etc

表示new_containervector<B>

有可能吗?

3 个答案:

答案 0 :(得分:3)

您可以尝试使用模板模板参数。

#include <vector>
#include <list>
#include <deque>
#include <string>

template <class T, class NewType>
struct rebind_sequence_container;

template <class ValueT, class Alloc, template <class, class> class Container, class NewType>
struct rebind_sequence_container<Container<ValueT, Alloc>, NewType >
{
     typedef Container<NewType, typename Alloc::template rebind<NewType>::other > type;
};

template <class Container, class NewType>
void test(const NewType& n)
{
    typename rebind_sequence_container<Container, NewType>::type c;
    c.push_back(n);
}

int main()
{
    std::string s;
    test<std::vector<int> >(s);
    test<std::list<int> >(s);
    test<std::deque<int> >(s);
}

但是,容器可能没有这两个模板参数。

此外,在容器适配器和关联容器中,不仅需要替换分配器(适配器中的基础容器,std::set中的谓词)。 OTOH,它们的用法与序列容器有很大的不同,很难想象一个适用于任何容器类型的模板。

答案 1 :(得分:2)

因为我试图从根本上解决同样的问题而偶然发现了这一点。它甚至可以在没有依赖于rebind特定的std::allocator类型的情况下工作 - 唯一的要求是反弹值类型是第一个模板参数各自的课程。所有相关的STL类(std::vectorstd::setstd::list等以及std::lessstd::allocator)就是这种情况。

预C ++ 11解决方案如下所示:

template <class Container, class NewType>
struct rebind;

template <class ValueType, template <class> class Container, class NewType>
struct rebind<Container<ValueType>, NewType>
{
  typedef Container<NewType> type;
};

template <class ValueType, class A, template <class, class> class Container, class NewType>
struct rebind<Container<ValueType, A>, NewType>
{
  typedef Container<NewType, typename rebind<A, ValueType>::type> type;
};

template <class ValueType, class A, class B, template <class, class, class> class Container, class NewType>
struct rebind<Container<ValueType, A, B>, NewType>
{
  typedef Container<NewType, typename rebind<A, ValueType>::type, typename rebind<B, ValueType>::type> type;
};

// Continue for more parameters (A, B, C, ...)

C ++ 11使它更容易:

template <class Container, class NewType>
struct rebind;

template <class ValueType, class... Args, template <class...> class Container, class NewType>
struct rebind<Container<ValueType, Args...>, NewType>
{
  typedef Container<NewType, typename rebind<Args, NewType>::type...> type;
};

为了支持std::array,可以添加以下内容:

template <class ValueType, std::size_t N, template <class, std::size_t> class Container, class NewType>
struct rebind<Container<ValueType, N>, NewType>
{
  typedef Container<NewType, N> type;
};

结果可以与任何STL类型一起使用:

#include <iostream>
#include <typeinfo>
#include <vector>
#include <set>
#include <deque>
#include <queue>
#include <list>
#include <array>

#include "rebind.h"

// Make it all a bit more compact
#define REBIND_DEMO(container, new_type)                \
  do {                                                  \
    container test;                                     \
    rebind<decltype(test), new_type>::type test2;       \
    std::cout << typeid(test).name() << "\n";           \
    std::cout << typeid(test2).name() << "\n";          \
  } while (0)

int main()
{
  REBIND_DEMO(std::set<float>, double);
  REBIND_DEMO(std::list<float>, double);
  REBIND_DEMO(std::deque<float>, double);
  REBIND_DEMO(std::queue<float>, double);
  typedef std::array<float, 4> TestArray;
  REBIND_DEMO(TestArray, double);
  REBIND_DEMO(std::unordered_set<float>, double);

  return 0;
}

运行此命令并在Linux系统上通过c++filt -t管道输出为您提供

std::set<float, std::less<float>, std::allocator<float> >
std::set<double, std::less<double>, std::allocator<double> >
std::list<float, std::allocator<float> >
std::list<double, std::allocator<double> >
std::deque<float, std::allocator<float> >
std::deque<double, std::allocator<double> >
std::queue<float, std::deque<float, std::allocator<float> > >
std::queue<double, std::deque<double, std::allocator<double> > >
std::array<float, 4ul>
std::array<double, 4ul>
std::unordered_set<float, std::hash<float>, std::equal_to<float>, std::allocator<float> >
std::unordered_set<double, std::hash<double>, std::equal_to<double>, std::allocator<double> >

答案 2 :(得分:1)

你指的是(我相信)the Policy Clone idiom,使用重新绑定