| Summary: | Problem with captureless generic lambda and calling function object with arguments passed by reference | ||
|---|---|---|---|
| Product: | gcc | Reporter: | Karol Wozniak <karol.wozniak> |
| Component: | c++ | Assignee: | Not yet assigned to anyone <unassigned> |
| Status: | RESOLVED DUPLICATE | ||
| Severity: | normal | CC: | daniel.kruegler, webrown.cpp |
| Priority: | P3 | Keywords: | c++-lambda |
| Version: | 6.1.1 | ||
| Target Milestone: | --- | ||
| Host: | Target: | ||
| Build: | Known to work: | 5.3.1 | |
| Known to fail: | Last reconfirmed: | ||
| Bug Depends on: | |||
| Bug Blocks: | 54367 | ||
minimal reproducible example
void foo(int &) {}
int main() {
int a;
[](auto &&x) { foo(static_cast<decltype(x) &&>(x)); }(a);
return 0;
}
$ g++-6 -std=c++14 prog.cc
prog.cc: In instantiation of ‘main()::<lambda(auto:1&&)> [with auto:1 = int]’:
prog.cc:5:14: required by substitution of ‘template<class auto:1> main()::<lambda(auto:1&&)>::operator decltype (((main()::<lambda(auto:1&&)>)0u).operator()(static_cast<auto:1&&>(<anonymous>))) (*)(auto:1&&)() const [with auto:1 = int]’
prog.cc:5:58: required from here
prog.cc:5:21: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
[](auto &&x) { foo(static_cast<decltype(x) &&>(x)); }(a);
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:1:6: note: initializing argument 1 of ‘void foo(int&)’
void foo(int &) {}
^~~
it seems to be same as: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70942 |
#include <utility> struct do_some_stuff_by_ref { template <typename... T> int operator()(T &...) const { return 42; }; }; int main() { { // compiles both // g++-5 prog.cc -std=c++14 // g++-6 prog.cc -std=c++14 do_some_stuff_by_ref callable{}; int a = 0; [callable](auto &&... x) { return callable(std::forward<decltype(x)>(x)...); }(a); } { // compiles both // g++-5 prog.cc -std=c++14 // g++-6 prog.cc -std=c++14 do_some_stuff_by_ref ignore_me{}; int a = 0; [ignore_me](auto &&... x) { (void)ignore_me; do_some_stuff_by_ref callable{}; return callable(std::forward<decltype(x)>(x)...); }(a); } { // compiles // g++-5 prog.cc -std=c++14 // fails to compile // g++-6 prog.cc -std=c++14 // prog.cc:40:22: error: no match for call to ‘(do_some_stuff_by_ref) (int)’ // return callable(std::forward<decltype(x)>(x)...); // ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int a = 0; [](auto &&... x) { do_some_stuff_by_ref callable{}; return callable(std::forward<decltype(x)>(x)...); }(a); } return 0; } // $ g++-5 --version // g++-5 (Debian 5.3.1-17) 5.3.1 20160429 // // $ g++-6 --version // g++-6 (Debian 6.1.1-1) 6.1.1 20160430