-
-
Save zed/bd7c7d7da73aac8183ea to your computer and use it in GitHub Desktop.
function template specialization via boost::enable_if<>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/a/9442727/ | |
#include <iostream> | |
#include <boost/type_traits/is_arithmetic.hpp> | |
#include <boost/type_traits/is_pointer.hpp> | |
#include <boost/utility/enable_if.hpp> | |
namespace { | |
template <class T> typename boost::enable_if< boost::is_pointer<T>, T>::type | |
GetFromStack() { | |
std::cout << "pointer" << std::endl; | |
return 0; | |
} | |
template <class T> typename boost::enable_if< boost::is_arithmetic<T>, T>::type | |
GetFromStack() { | |
std::cout << "arithmetic" << std::endl; | |
return 0; | |
} | |
template <> short | |
GetFromStack<short>() { | |
std::cout << "short" << std::endl; | |
return 0; | |
} | |
} | |
int main() { | |
GetFromStack<int>(); // -> arithmetic | |
GetFromStack<short>(); // -> short | |
GetFromStack<int*>(); // -> pointer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment