I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression
SFINAE only works when substitution failure happens in the immediate context. The template parameter Index is already known by the time the member function template test is being instantiated, so instead of substitution failure you get a hard error.
The trick to working around this is to deduce Index again by adding an additional template type parameter to test and default it to Index.
template())[std::declval()] // and use that here
),
class = typename std::enable_if<
!std::is_void::value
>::type>
static std::true_type test(int);
Now your code works as intended.
Live demo