https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79513
Bug ID: 79513 Summary: std::visit doesn't handle references Product: gcc Version: 7.0.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: benni.buch at gmail dot com Target Milestone: --- #include <variant> int main(){ std::variant<int> v(5); std::visit([](int&){}, v); std::visit([](int&&){}, std::move(v)); } Should be a valid program, but libstdc++ discards in both cases the references: In file included from main.cpp:1:0: /usr/local/include/c++/7.0.1/variant: In instantiation of 'constexpr decltype(auto) std::visit(_Visitor&&, _Variants&& ...) [with _Visitor = main()::<lambda(int&&)>; _Variants = {std::variant<int>}]': main.cpp:6:38: required from here /usr/local/include/c++/7.0.1/variant:1266:44: error: no match for call to '(main()::<lambda(int&&)>) (std::variant_alternative_t<0, std::variant<int> >&)' decltype(std::forward<_Visitor>(__visitor)(get<0>(__variants)...)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/7.0.1/variant:1266:44: note: candidate: void (*)(int&&) <conversion> /usr/local/include/c++/7.0.1/variant:1266:44: note: conversion of argument 2 would be ill-formed: /usr/local/include/c++/7.0.1/variant:1266:44: error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'std::variant_alternative_t<0, std::variant<int> > {aka int}' main.cpp:6:21: note: candidate: main()::<lambda(int&&)> <near match> std::visit([](int&&){}, std::move(v)); ^ main.cpp:6:21: note: conversion of argument 1 would be ill-formed: In file included from main.cpp:1:0: /usr/local/include/c++/7.0.1/variant:1266:44: error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'std::variant_alternative_t<0, std::variant<int> > {aka int}' decltype(std::forward<_Visitor>(__visitor)(get<0>(__variants)...)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/7.0.1/variant:1266:44: error: no match for call to '(main()::<lambda(int&&)>) (std::variant_alternative_t<0, std::variant<int> >&)' /usr/local/include/c++/7.0.1/variant:1266:44: note: candidate: void (*)(int&&) <conversion> /usr/local/include/c++/7.0.1/variant:1266:44: note: conversion of argument 2 would be ill-formed: /usr/local/include/c++/7.0.1/variant:1266:44: error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'std::variant_alternative_t<0, std::variant<int> > {aka int}' main.cpp:6:21: note: candidate: main()::<lambda(int&&)> <near match> std::visit([](int&&){}, std::move(v)); ^ main.cpp:6:21: note: conversion of argument 1 would be ill-formed: In file included from main.cpp:1:0: /usr/local/include/c++/7.0.1/variant:1266:44: error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'std::variant_alternative_t<0, std::variant<int> > {aka int}' decltype(std::forward<_Visitor>(__visitor)(get<0>(__variants)...)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~