1

以下代码使用 g++-4.8 编译,但在使用 clang 3.4 时不编译。

#include <type_traits>
#include <functional>

struct A {
    template <typename Continuation>
    bool operator()(
            //const  Continuation & continuation
            Continuation continuation
        ) const {
        return true;
    }
};

bool  f(A)  {
    return true;
}

auto g(A a) ->
typename  std::result_of<A(
    decltype(std::bind(f, a)))>::type
{
    auto continuation = std::bind(f, a);

    return a(continuation);
}

int main(int argc, char ** argv) {
    A a;
    g(a);
}

g++-4.8 -std=c++0x test.cpp #OK

clang++ -std=c++0x test.cpp

test.cpp:22:38: error: no type named 'type' in 'std::result_of<A (std::_Bind<bool (*(A))(A)>)>'
    decltype(std::bind(f, a)))>::type
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
1 error generated.

当您取消注释注释行并注释以下行时,代码将在两个 clang ang g++ 上编译。

4

1 回答 1

1

result_of 在 decltype 之前,您应该像这样简化语法:

auto g(A a) -> decltype( std::declval<A>()( std::bind(f, a) ) )
于 2014-03-24T14:44:28.860 回答