Boost logo

Boost :

Subject: Re: [boost] is_range metafunction
From: Daniel Walker (daniel.j.walker_at_[hidden])
Date: 2008-10-06 20:56:33


On Mon, Oct 6, 2008 at 6:58 AM, Mathias Gaunard
<mathias.gaunard_at_[hidden]> wrote:
> 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:

So, I tried your suggestion with gcc 4.4, but I got an error.

#include <cstddef>

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();
}

struct t0 {
    int foo() { return 0; }
};

int main()
{
        t0 x;
        foo(x); // error: no matching function for call to 'foo(t0&)'
}

If I take away the && in foo(), I get ICE.

Any idea what's wrong here?

Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk