I've seen this expression in a library implementation and I basically understand it is been used to foster SFINAE or even to pull the static_assert
trigger.
It basically takes the form:
template <typename>
char (&checkValid(...))[2];
template <typename T>
char checkValid(e); where e is an expression(using type T) results in type X
If e
is well-formed then the result will be (assuming the usage of sizeof
) 1 else 2 and can be applied in:
static_assert(sizeof(checkValid<T>(0))==1,"") ;
The other day I've been doing something similar in a different way:
using namespace std;
template<typename...T>
using isValid = void;
template<typename>
false_type checkValid(...);
template<typename T>
true_type checkValid(isValid<typename T::type>*);
struct some{
using type = int;
};
int main(){
constexpr bool result = decltype(checkValid<some>(0))::value;
}
Regardless of what I've done and what I saw, I am more curious about knowing:
What is this expression called?
template <typename>
char (&checkValid(...))[2];
"Variable template"? "Function template?" Or "an array taking the reference to ..."? (sorry if my guess is awful)