When writing generic code, it is sometimes useful to check whether or not a particular SFINAE-friendly expression is valid (e.g. to branch at compile-time). Let’s assume that we have the following class declarations…
struct Cat
{
void meow() const { cout << "meow\n"; }
};
struct Dog
{
void bark() const { cout << "bark\n"; }
};…and that we would like to write a template function
make_noise(x) that calls x.meow() and/or
x.bark() if they are well-formed expressions:
template <typename T>
void make_noise(const T& x)
{
// Pseudocode:
/*
if(`x.meow()` is well-formed)
{
execute `x.meow();`
}
else if(`x.bark()` is well-formed)
{
execute `x.bark();`
}
else
{
compile-time error
}
*/
}