In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e.
template<int N> struct A {
int sum() const noexcept
{ return _sum<N-1>(); }
private:
int _data[N];
template<int I> typename std::enable_if< I,int>::type _sum() const noexcept
{ return _sum<I-1>() + _data[I]; }
template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept
{ return _data[I]; }
};
However, this won't work if the function in question (_sum()
in above example) has auto-detected return type, such as _func()
in this example
template<int N> class A
{
/* ... */
private:
// how to make SFINAE work for _func() ?
template<int I, typename BinaryOp, typename UnaryFunc>
auto _func(BinaryOp op, UnaryFunc f) const noexcept -> decltype(f(_data[0]))
{ return op(_func<I-1>(op,f),f(_data[I])); }
};
What else can be done to get SFINAE here?