I have simplified the solution here to help me determine if a class has a member function:
template<typename T>
struct HasTest{
template<typename R, typename S = decltype(declval<R>().test())> static true_type Test(R*);
template<typename R> static false_type Test(...);
using def = decltype(Test<T>(0));
};
I need to use HasTest<T>::def::value
in the condition of a conditional_t
. The problem is I have to do this with quite a number of functions, and since I need to declare a struct per function I was hoping to find a way to do the Substitution Fail Is Not An Error(SFINAE) within the condition. Something like:
conditional_t<struct { template<typename R, typename S = decltype(declval<R>().test())> static true_type Test(R*); template<typename R> static false_type Test(...); using def = decltype(Test<T>(0)); }::def, true_type, false_type>
That example clearly doesn't compile but hopefully it makes what I'm trying to do clear.
Right now I have to set up a struct for each method I plan to test for in namespace details
and then use them in my conditional_t
. This introduces pollution and separates the inner workings of the SFINAE from the conditional_t
s it's used in.
Does C++14/17 provide me an alternative way to do this, or is there a way for me to declare and use an anonymous struct
in the conditional_t
condition?
I've created a simple test here that you're welcome to try things out on.