15

ここでマシューの答えを読んだ後、私はこれを自分で試すことにしました。

SFINAE が起動しhas_fooてアクセスしようとする関数を選別しないため、私の試みはコンパイルに失敗しますT::foo

error: ‘struct Bar’ has no member named ‘foo’

私は何かを見逃していますか、それとも私がこのようにしようとしていることは不可能ですか?

(私は gcc-4.7.2 を使用しています)

以下の完全な例:

#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;
}
4

1 に答える 1

12

ここでの AFAICS の主な問題は、ランタイム参照をconstexpr関数パラメーターとして使用していることです。これを交換するとうまくいきます。

#include <iostream>

// culled by SFINAE if foo does not exist
template<typename T>
constexpr auto has_foo(int) -> decltype(std::declval<T>().foo, bool())
{
    return true;
}
// catch-all fallback for items with no foo
template<typename T> 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>(0)>::value(t);
}
//-----------------------------------------------------

struct Bar
{
    int val;
};
struct Foo {
    int foo;
};

int main()
{
    Bar b { 5 };
    Foo f { 5 };
    std::cout << get_foo(b) << std::endl;
    std::cout << get_foo(f) << std::endl;
    return 0;
}
于 2013-08-16T04:07:06.977 に答える