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
Palindrome Recursive Function Example
uniq ptr
ghfhfgh
Ordered Graphs
Program_3
Bubble Sort Example
HTML Timetable generator.cpp
Range List for C++
Wrapper to pass shared_ptr to &, const &, *, const * through std::bind.
INHERIT_CTORS default constructor