Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare an "unsigned T"

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?

like image 327
user877329 Avatar asked Apr 04 '13 08:04

user877329


People also ask

How do you declare unsigned?

The data type to declare an unsigned integer is: unsigned int and the format specifier that is used with scanf() and print() for unsigned int type of variable is "%u".

What is an unsigned int?

An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation. The most significant byte is 0 and the least significant is 3.

What is a unsigned integer example?

The simplest numbers that we want to represent in the machine are the unsigned integers. These are whole numbers without a sign, for example, 0, 1, 2, 3, … The mechanical calculators of yesteryear and the car mileage meter of today both store unsigned integers on what are effectively cogs having ten numbered teeth1.

What is unsigned operator in C?

An unsigned Integer means the variable can hold only a positive value. This format specifier is used within the printf() function for printing the unsigned integer variables. Syntax: printf(“%u”, variable_name);


2 Answers

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

like image 92
Jerry Coffin Avatar answered Oct 01 '22 10:10

Jerry Coffin


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

like image 20
4pie0 Avatar answered Oct 01 '22 10:10

4pie0