クラステンプレートをメタ関数で特殊化したい
Fusion! Boost.Geometry - Togetter ここら辺から派生した話。
素直にenable_ifを使えばできます。ただし複数の特殊化にマッチした場合はambiguousでコンパイルエラー
#include <iostream> #include <string> #include <typeinfo> #include <cxxabi.h> #include <stdlib.h> #include <boost/type_traits.hpp> #include <boost/utility/enable_if.hpp> #ifdef __GNUC__ std::string demangle(const char *mangled) { int status; char *const p = abi::__cxa_demangle(mangled, 0, 0, &status); const std::string s(p); free(p); return s; } #else std::string demangle(const char *mangled) { return mangled; } #endif template <typename T, typename = void> struct F { std::string name() const { return "generic F<T> [with T=" + demangle(typeid(T).name()) + "]"; } }; template <typename T> struct F<T, typename boost::enable_if< boost::is_arithmetic<T> >::type> { std::string name() const { return "specialized F<arithmetic_type>"; } }; template <typename T> struct F<T, typename boost::enable_if< boost::is_integral<T> >::type> { std::string name() const { return "specialized F<integral_type>"; } }; int main() { //std::cout << F<int>().name() << '\n'; //ambiguous std::cout << F<double>().name() << '\n'; }