user1415913
user1415913

Reputation: 335

How can I determine the container template type of an instantiated container template in C++

I need collection_traits with which I can do something like

typename collection_traits<std::vector<int>>::template type<double> double_vector;

Now double_vector is of type std::vector<double>.

So what I want is to get the same container, but with different value type.

Is this even possible?

Upvotes: 1

Views: 98

Answers (1)

Bartosz Przybylski
Bartosz Przybylski

Reputation: 1656

Variadic template will help you https://ideone.com/lbS2W3

using namespace std;

template<typename... T>
struct collection_traits { using template_type = void; };

template<template<typename...> class C, typename... Args>
struct collection_traits<C<Args...> > {
  template<typename... Subst>
  using template_type = C<Subst...>;
};

int main(int argc, char *argv[]) {
  collection_traits<vector<int>>::template_type<double> t;

  cout << typeid(t).name() << endl;

  t.push_back(123.5);
  cout << t.front() << endl;

}

Upvotes: 3

Related Questions