After reading Matthieu's answer here, I decided to try this myself.
My attempt fails to compile because SFINAE doesn't kick in and cull the has_foo
function which attempts to access T::foo
.
error: ‘struct Bar’ has no member named ‘foo’
Am I missing something, or is what I'm attempting to do not possible in this way?
(I'm using gcc-4.7.2)
Full examplar below:
#include <iostream>
// culled by SFINAE if foo does not exist
template<typename T>
constexpr auto has_foo(T& t) -> decltype((void)t.foo, bool())
{
return true;
}
// catch-all fallback for items with no foo
constexpr bool has_foo(...)
{
return false;
}
//-----------------------------------------------------
template<typename T, bool>
struct GetFoo
{
static int value(T& t)
{
return t.foo;
}
};
template<typename T>
struct GetFoo<T, false>
{
static int value(T&)
{
return 0;
}
};
//-----------------------------------------------------
template<typename T>
int get_foo(T& t)
{
return GetFoo<T, has_foo(t)>::value(t);
}
//-----------------------------------------------------
struct Bar
{
int val;
};
int main()
{
Bar b { 5 };
std::cout << get_foo(b) << std::endl;
return 0;
}
The primary issue here AFAICS is that you are using the run-time reference as a
constexpr
function parameter. Replacing this works just fine.