How do I accomplish the following in C++, and what is doing such things called?
template <bool S>
class NuclearPowerplantControllerFactoryProviderFactory {
// if S == true
typedef int data_t;
// if S == false
typedef unsigned int data_t;
};
There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.
Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
C++ Class Template with multiple parameters You can also use multiple parameters in your class template.
By specialization:
template <bool> class Foo;
template <> class Foo<true>
{
typedef int data_t;
};
template <> class Foo<false>
{
typedef unsigned int data_t;
};
You can choose to make one of the two cases the primary template and the other one the specialization, but I prefer this more symmetric version, given that bool
can only have two values.
If this is the first time you see this, you might also like to think about partial specialization:
template <typename T> struct remove_pointer { typedef T type; };
template <typename U> struct remove_pointer<U*> { typedef U type; };
As @Nawaz says, the easiest way is probably to #include <type_traits>
and say:
typedef typename std::conditional<S, int, unsigned int>::type data_t;
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