|
-
August 4th, 2010, 05:11 AM
#13
Re: signed to unsigned (and vice versa)
 Originally Posted by monarch_dodra
You seem to be implying there is a way to do it with C++0x/boost, what's on your mind?
you can use c++0x/boost type traits libraries to save on typing all those specializations 
BTW, thinking about it, if you need automatic type deduction and a finer control over function overloads, then you might want something like
Code:
template <typename T> struct enable_signed {};
template <typename T> struct enable_unsigned {};
template <> struct enable_signed<signed int> { typedef int type; };
template <> struct enable_unsigned<unsigned int> { typedef int type; };
// ...
template <class U,class V, class enable_U = typename enable_signed<U>::type, class enable_V = typename enable_unsigned<V>::type >
struct enable_signed_unsigned {};
template <class U,class V, class enable_U = typename enable_unsigned<U>::type, class enable_V = typename enable_signed<V>::type >
struct enable_unsigned_signed {};
// ..
template<class U,class V> enable_signed_unsigned<U,V> f( U u, V v );
template<class U,class V> enable_unsigned_signed<U,V> f( U u, V v );
// or template<class U,class V> void f( U u, V v, enable_signed_unsigned<U,V> ignoreme = enable_signed_unsigned<U,V>() );
// or use some sort of dispatcher ...
int main()
{
f(1,1u); // calls first f
f(1u,1); // calls second f
f(1u,1u); // error
f(1,1); // error
}
ps. I just quickly tested the above with the online Comeau compiler ... so you should check it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|