Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to extract in c++ the container template class?

I was wondering if it is possible to detect the template class container type, and redefine its parameters. For example :

typedef std::vector<int> vint;
typedef typeget<vint>::change_param<double> vdouble;

Where vdouble would now be an std::vector<double>?

like image 500
g24l Avatar asked Oct 22 '15 13:10

g24l


People also ask

How is a template class different from a template function?

For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.

Is vector a class template or object template?

vector is a template class, which can be instantiated with a type, in the format: vector<int> , vector<double> , vector<string> . The same template class can be used to handle many types, instead of repeatably writing codes for each of the type.

What does template typename t mean?

template <typename T> ... This means exactly the same thing as the previous instance. The typename and class keywords can be used interchangeably to state that a template parameter is a type variable (as opposed to a non-type template parameter).

How to define template class in Cpp?

Templates in C++ is an interesting feature that is used for generic programming and templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Simply put, you can create a single function or single class to work with different data types using templates.


1 Answers

Adding on to @Kerrek SB's answer, here is the generic approach:

template<typename...> struct rebinder;

template<template<typename...> class Container, typename ... Args>
struct rebinder<Container<Args...>>{
    template<typename ... UArgs>
    using rebind = Container<UArgs...>;
};

Which will work for any container under the sun.

like image 161
RamblingMad Avatar answered Sep 18 '22 15:09

RamblingMad