c++ - How to dispatch to templated call operator using SFINAE -
i have function takes templated callable argument, , passes index it. in situations, index passed statically (i'm working tuples). thought should possible passing callable object templated call operator , using sfinae.
at first, looks like:
struct { template< size_t > void operator()( int x ) { cout << "a " << x << " " << << endl; } }; struct b { void operator()( int x, int ) { cout << "b " << x << " " << << endl; } }; template< typename f, size_t = 0 > inline void call( int x, f & fn ) { fn( x, ); } int main() { a; b b; call( 2, b ); call< b, 3 >( 2, b ); call( 1, ); // no match call '(a) (int&, long unsigned int)' return 0; }
so try overload call function , select right invocation using sfinae:
template< typename f, size_t = 0 > inline typename std::enable_if< /* i've tried kinds of things here */ >::type call( int x, f & fn ) { fn< >( x ); }
but can't figure out type traits detect whether f callable 1 template parameter , int argument. i've been referencing this article , this one having trouble adapting them use case. ideas? possible without modifying call site?
struct { template< std::size_t > void operator()( int x, std::integral_constant<std::size_t, i> ) { cout << "a " << x << " " << << endl; } };
use instead. standard sfinae tests work, no passing template nontype arguments.
in c++14 compiler can std::get<i>
. in c++11 compiler can std::get<i>
or std::get<decltype(i)::value>
.
passing template non-type arguments sucks. avoid them.
Comments
Post a Comment