
6 Oct
2008
6 Oct
'08
10:58 a.m.
Daniel Walker wrote:
That's great news! That would do the trick!
Indeed, it's pretty cool. In C++0x, if you write template<typename T> auto foo(T&& t) -> decltype(t.foo()) { return t.foo(); } Then that definition is only visible if T has a nullary foo member function. It's basically the same as type inference in functional programming. As of today, in GCC 4.4, you can do something equivalent but have to be significantly more verbose: template<typename T, size_t Cond> struct foo_type { typedef decltype(((T*)0)->foo()) type; }; template<typename T> typename foo_type<T, sizeof(((T*)0)->foo() > 0)>::type foo(T&& t) { return t.foo(); } (You cannot put decltype directly in the return type because of name mangling issues)