473,508 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Upsigning or downsigning

Is there any way to manipulate a template parameter to change its
signedness?

For example,

template <typename T>
void foo(signed T a, unsigned T b) {

}

defines a templated function that is instantiated with a base numeric
type (e.g. int or long) but is referred by signed and unsigned
variants.

Also, how do I convert a typename to signed or unsigned? For example,
if I had some variable k I wanted to cast to the unsigned variant of
type T, I'd like to be able to do

j = (unsigned_type(T))k

but I'm not sure if it's possible to do something like that. Is there
anyway to "upsign" (i.e. convert a numeric type to signed) or
"downsign" (i.e. convert a numeric type to unsigned) similar to how
one might upcast or downcast a number?

Thanks!

Damian Eads
Apr 7 '08 #1
1 1204
On 7 Apr, 17:40, Damian <damian.e...@gmail.comwrote:
Also, how do I convert a typename to signed or unsigned? For example,
if I had some variable k I wanted to cast to the unsigned variant of
type T, I'd like to be able to do

* *j = (unsigned_type(T))k

but I'm not sure if it's possible to do something like that. Is there
anyway to "upsign" (i.e. convert a numeric type to signed) or
"downsign" (i.e. convert a numeric type to unsigned) similar to how
one might upcast or downcast a number?
How about this ?
<code>
template<typename Tstruct sign_variants {};
#define MAKE_SVS_FOR(T) \
template<struct sign_variants<T{\
typedef T signed_t;\
typedef unsigned T unsigned_t;\
};\
template<struct sign_variants<unsigned T{\
typedef T signed_t;\
typedef unsigned T unsigned_t;\
};
MAKE_SVS_FOR(char)
// hmmm ... glossing over char vs signed char vs unsigned char ...
MAKE_SVS_FOR(int)
MAKE_SVS_FOR(long)
MAKE_SVS_FOR(short)
// add any other flavours your compiler supports

#undef MAKE_SVS_FOR

// then e.g.
#include <iostream>

const char* foo(long x)
{ return "slong"; }
const char* foo(unsigned long x)
{ return "ulong"; }

void signedness()
{
std::cout << foo(sign_variants<long>::signed_t()) << '\n';
std::cout << foo(sign_variants<long>::unsigned_t()) << '\n';
std::cout << foo(sign_variants<unsigned long>::signed_t()) << '\n';
std::cout << foo(sign_variants<unsigned long>::unsigned_t()) <<
'\n';
}
/*
output
slong
ulong
slong
ulong
*/
</code>

Obviously you'd use sign_variants inside a template.
Apr 7 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.