lo tolmencre
lo tolmencre

Reputation: 3934

Convert Template Parameter in Typedef

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

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93264

You're looking for std::make_unsigned:

template <class X>
class C
{
    using Y = std::make_unsigned_t<X>;
};

Upvotes: 2

Related Questions