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>
?
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.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With