複数条件での SFINAE
template<typename T> void func(typename boost::disable_if<boost::is_pod<T> >::type* = 0){ std::cout << "is no POD" << std::endl; } template<typename T> void func(typename boost::enable_if<boost::is_pod<T> >::type* = 0){ std::cout << "is POD" << std::endl; } func(10); // is POD func("test"); // is POD func(std::string("aaaaa")); // is not POD
こんな感じで、enable_if と SFINAE を利用して、型によって呼び出しを切り替えることが出来ます。
1つの型に対して、処理ならばいいんですが、複数の条件を当てはめるとちょっとややこしくなります。
template<typename T> void func( T value, typename boost::disable_if<boost::is_pod<T> >::type* = 0, typename boost::disable_if<boost::is_integral<T> >::type* = 0 ){ std::cout << "is no POD & integral" << std::endl; } template<typename T> void func( T value, typename boost::enable_if<boost::is_pod<T> >::type* = 0, typename boost::disable_if<boost::is_integral<T> >::type* = 0 ){ std::cout << "is POD" << std::endl; } template<typename T> void func( T value, // typename boost::disable_if<boost::is_pod<T> >::type* = 0, // is_integra は、POD 型なので typename boost::enable_if<boost::is_integral<T> >::type* = 0 ){ std::cout << "is integral" << std::endl; } func(10); // is integral func("test"); // is POD func(std::string("aaaaa")); // is not POD & integral
うーん……、何かうまい書き方はないものか。