I have just begun dabbling with SFINAE and I am having trouble understanding the syntax behind the most often-used example which appears in various forms, but the idea is to check whether a particular type contains a given member. This particular one is from Wikipedia:
template <typename T> struct has_typedef_foobar
{
typedef char yes[1];
typedef char no[2];
template <typename C> static yes& test(typename C::foobar*);
template <typename> static no& test(...);
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
There are a couple of things I don't get about this:
template <typename T> func(int T::*arg)
{
*arg = 1;
}
struct Foo
{
int x;
} foo;
func<Foo>(&foo::x); // something like this?
func(&foo::x); // or maybe even like this?
Most of these questions have nothing to do with SFINAE:
typename. Since C is a template parameter to test(), clearly C::foobar is a dependent name. Even though function declarations require a type in front of each argument, the language requires the use of typename to turn the dependent name C::foobar into a type. With that, typename C::foobar is just a type and applying a the type constructor * to it, produces the corresponding pointer type.int C::* is an unnamed pointer to data member of type int.template can be omitted if it isn't used. Most of the time it is used in some form, though, in which case it is, obviously, required.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With