Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Specialization on signed types
//Title of this code #include <iostream> #include <typeinfo> template <typename T> using EnableSigned = typename std::enable_if< std::is_signed<T>::value >::type* ; template <typename T> using EnableUnSigned = typename std::enable_if< std::is_unsigned<T>::value >::type* ; template<class T , EnableSigned<T> = nullptr > T myabs(T num) { return (num > 0) ? num : num*-1; } template<class T , EnableUnSigned<T> = nullptr > T myabs(T num) { return num; } // the partial specialization of A is enabled via a template parameter template<class T, class Enable = void> class A {}; // primary template template<class T> class A<T, typename std::enable_if<std::is_signed<T>::value>::type> { public: A() {printf("Signed Value\n");} }; // specialization for signed types template<class T> class A<T, typename std::enable_if<std::is_unsigned<T>::value>::type> { public: A() {printf("Unsigned Value\n");} }; // specialization for unsigned types int main() { unsigned int x = 90; int y = 100; A<int> myA; A<unsigned int> myB; printf("myabs = %d\n", myabs(x)); printf("myabs = %d\n", myabs(y)); }
run
|
edit
|
history
|
help
0
Arduino Bluetooth Periodiek Systeem
MPL 2-0
std::function ambiguity clang
non-template template parameters for container stream insertion: SFINAE
regimeketopdfb
Throttle Example using circular queue (push all but 2 less than maxSize; then pop all but 2 less than current size)
C++ Operator Overloading
Derivation of the 0x9E3779B97F4A7C17u constant
template specialization inheritance problem
Unpacking tuple