8

I want to use unsigned T in a template:

template<class T>
void signed2unsigned(const T* source, unsigned T* dest, size_t n)
    {
    do
        {
        intmax_t s=*source;
        s+=min(*source);

        *dest=s;

        ++dest;
        ++source;
        --n;
        }
    while(n!=0);
    }

This doesn't work, since unsigned without is expanded to unsigned int before the type T is taken into account. Is there a workaround other than just introduce U and write in documentation that U has to be an unsigned T?

user877329
  • 6,717
  • 8
  • 46
  • 88

2 Answers2

11

std::make_unsigned<T>::type is what you seem to be looking for.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

you can use std::make_unsigned<T>::type

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • except that g++ (tdm64-1) 4.7.1 does not find it in template void signed2unsigned(const T* source, std::make_unsigned::type* dest, size_t n) and I have -std=c++11 on gives 'std::make_unsigned::type' is not a type – user877329 Apr 04 '13 at 08:43
  • use a typedef std::make_unsigned::type T_type – 4pie0 Apr 04 '13 at 08:49
  • Eventhough it compiles, it actually breaks the idea of using a template since I cannot make a typedef as a template. – user877329 Apr 04 '13 at 08:53
  • @user877329 You want to restrict the `dest` to an unsigned-like types (`uint8_t`, `uint16_t`, `uint32_t`, `uint64_t`)? – Red XIII Apr 04 '13 at 12:05
  • well I think this is a topic for a new question – 4pie0 Apr 06 '13 at 11:34