c++ - std::make_signed that accepts floating point types -


i have templated class can instantiated scalar types (integers, floats, etc.) , want member typedef signed variant of type. is:

unsigned int -> signed int
signed long long -> signed long long (already signed)
unsigned char -> signed char
float -> float
long double -> long double
etc...

unfortunately, std::make_signed works integral types, not floating point types. simplest way this? i'm looking of form using signedt = ...;, part of templated class template parameter t guaranteed scalar.

a simple template alias do:

#include <type_traits>  template<typename t> struct identity { using type = t; };  template<typename t> using try_make_signed =     typename std::conditional<         std::is_integral<t>::value,         std::make_signed<t>,         identity<t>         >::type; 

and how test it:

int main() {     static_assert(::is_same<         try_make_signed<unsigned int>::type, int         >::value, "!");      static_assert(std::is_same<         try_make_signed<double>::type, double         >::value, "!"); } 

here live example.


Comments

Popular posts from this blog

Winapi c++: DialogBox hangs when breaking a loop -

vb.net - Font adding using PDFsharp -

javascript - jQuery iScroll clickable list elements while retaining scroll? -