使用g ++ 6.1可能的std :: forward回归 - 错误或预期的行为?

Vit*_*meo 10 c++ lambda gcc perfect-forwarding c++14

g ++ 6.1最近被引入到Arch Linux的测试库中,我用g ++ 5.3.0成功编译的一些代码不再编译了.我做了一个最小的例子:

gcc.godbolt.org链接

// This code compiles with g++ 5.3.0
// This does not compile with g++ 6.1

#include <type_traits>
#include <utility>
#include <tuple>

#define FWD(...) ::std::forward<decltype(__VA_ARGS__)>(__VA_ARGS__)

struct sinker
{
    template <typename T>
    void sink(T&)
    {
    }
};

template <typename T, typename TF>
void caller(T& v, TF&& f)
{
    sinker s;
    f(s, v);
}

template <typename T>
void interface(T& v)
{
    return caller(v, [](auto& xs, auto&& xv) -> decltype(auto)
        {
            xs.sink(FWD(xv));
        });
}

int main()
{
    int x = 0;
    interface(x);
}
Run Code Online (Sandbox Code Playgroud)

这是报告的错误:

: In instantiation of ‘get_impl(T&)::<lambda(auto:1&, auto:2&&)> [with auto:1 = sinker; auto:2 = int; T = int]’:
:25:58:   required by substitution of ‘template<class auto:1, class auto:2> get_impl(T&)
      [with T = int]::<lambda(auto:1&, auto:2&&)>::operator 
      decltype (((get_impl(T&) [with T = int]::<lambda(auto:1&, auto:2&&)>)0u).operator()(static_cast<auto:1&>(<anonymous>),
       static_cast<auto:2&&>(<anonymous>))) (*)(auto:1&, auto:2&&)() const [with auto:1 = sinker; auto:2 = int]’
:19:6:   required from ‘void chunk_fn_impl(T&, TF&&) [with T = int; TF = get_impl(T&) [with T = int]::<lambda(auto:1&, auto:2&&)>]’
:25:25:   required from ‘void get_impl(T&) [with T = int]’
:36:15:   required from here
:27:13: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
             xs.sink(FWD(md));
             ^~
:10:10: note:   initializing argument 1 of ‘void sinker::sink(T&) [with T = int]’
     void sink(T&)
          ^~~~
Run Code Online (Sandbox Code Playgroud)

更改:

return caller(v, [](auto& xs, auto&& xv) -> decltype(auto)
Run Code Online (Sandbox Code Playgroud)

至:

return caller(v, [](auto& xs, auto& xv) -> decltype(auto)
Run Code Online (Sandbox Code Playgroud)

允许代码成功编译.


我不明白为什么会发生这种错误,因为xv它被完美地转发并且FWD(xv)调用应该产生左值引用.请注意,代码在g ++ 5.3.0和clang ++ 3.7中按预期工作.

gcc.godbolt.org链接

(尝试使用多个g ++版本进行编译并更改auto&&auto&.)

这是一个g ++ 6.1错误吗?或者代码是否与之前版本的g ++和clang ++错误地编译?

Vit*_*meo 1

这是一个 g++ 前端错误:该问题被报告为错误 70942