追記:2011/04/14
n3281の変更により、Variadic template parameterは、明示的な実引数があるものだけ、partial orderingで考慮されるようになった。つまり、この変更により、本記事の内容は現状と一致しない。
つまり、以下のようなことができる。
template<class T>
auto f_impl( T x ) -> decltype ( x.func() )
{
std::cout << "has T::func()" << std::endl ;
return x.func() ;
}
template<class T, typename ... Types >
T f_impl( T x, Types ... args )
{
std::cout << "no T::func()" << std::endl ;
return x ;
}
template < typename T >
auto f( T x ) -> decltype( f_impl(x) )
{
return f_impl( x ) ;
}
struct Foo{ int func() { return 0 ; } } ;
struct bar {} ;
int main()
{
f( Foo() ) ; // has T::func()
f( Bar() ) ; // no T::func()
f( 0 ) ; // no T::func()
}
素晴らしい。
No comments:
Post a Comment