template<class T>
void foo(T s)
{
unsigned T x = -1;
//...
}
{
int x = 129;
foo(x);
}
How can I modify template type to be unsigned? I know that this is bad practice due to the fact that not everything can be declared unsigned, but in special cases it makes sense.
I do not want to pass the modified type as a template argument. I want to modify the type in the function body itself.
You can use
std::make_unsignedto retrieve the unsigned type corresponding to an integral type.Do note that your program will be ill-formed / exhibit undefined behavior if the template argument
Tisn't one of the enumerated valid types thatstd::make_unsignedaccepts.