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?
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".
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.
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.
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);
std::make_unsigned<T>::type
is what you seem to be looking for.
you can use std::make_unsigned<T>::type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With