Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
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
Please
log in
to post a comment.
IsContainer
C++ Template
ghfhfgh
SceneGraph Interviewee Task
pointer to complete array does not convert implicitly to pointer to array of unknown bound
hw1 Os
general
DESim Example with Hash Table
Forgetting to check end
overloading
stackse - search stackoverflow differently
Please log in to post a comment.