Skip to content

Instantly share code, notes, and snippets.

@zed

zed/kyle.cpp Secret

Created February 25, 2012 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/bd7c7d7da73aac8183ea to your computer and use it in GitHub Desktop.
Save zed/bd7c7d7da73aac8183ea to your computer and use it in GitHub Desktop.
function template specialization via boost::enable_if<>
// 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