as a follow up to this question, how can the code be changed so that i can use it in the constructor of a class? im writing a new class where the input needs to be an number of some kind, but nothing else. the code, however, is like declaring the type in front of a function. since constructors dont exactly have types, i need it to not declare a type for the function itself
my new class:
class C{
public:
C();
C(T value);// specifically looking for this
T f(T value); // what the code currently does
};
the code in the link creates a function that [accepts and] returns an integer type T. i need it to not return anything at all, so that it can be used with a constructor
I think you want to restrict types for constructor template. If so, then you can do this:
This constructor template can accept only those
Tfor whichis_arithmetic<T>::valueistrue. The implementation ofenable_ifis exactly same, as given in the other answer.Alternatively, or if you don’t have
type_traits, then you can usetypelistalong withenable_if. I think this is a better solution, as you can specifically define the supported typelist.This constructor template can accept only those
Tfor whichexists<T,supported_types>::valueistrue. Theexistsmetafunction checks whetherTis existing in the typelistsupported_typesor no. You can add more types to this typelist.And the implementation of
typelist, andexistsis here (see my solution):