Reputation: 3934
I am writing a class that requires the template type it gets instantiated with to behave like unsigned types under bitshift operations. This is so, because negative numbers under bitshift behave differently from positive number under bitshift operations. My idea therefore was to simply convert whatever type I get as a template parameter to an unsigned version of it in a typedef and internally only use the typedefed version. If the template parameter type does not support this, then... it simply doesn't and there would be a compile time error. What I tried looks like this:
template <class X>
class C
{
using Y = unsigned X;
};
which is incorrect: error: type-id cannot have a name
Is this at all possible and if so how?
Upvotes: 1
Views: 183
Reputation: 93264
You're looking for std::make_unsigned
:
template <class X>
class C
{
using Y = std::make_unsigned_t<X>;
};
Upvotes: 2