Bug 71095 - Problem with captureless generic lambda and calling function object with arguments passed by reference
Summary: Problem with captureless generic lambda and calling function object with argu...
Status: RESOLVED DUPLICATE of bug 71117
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2016-05-13 07:56 UTC by Karol Wozniak
Modified: 2022-03-11 00:32 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 5.3.1
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Karol Wozniak 2016-05-13 07:56:20 UTC
#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
Comment 1 Karol Wozniak 2016-05-13 20:49:13 UTC
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 &) {}
      ^~~
Comment 2 Karol Wozniak 2016-05-14 10:09:44 UTC
it seems to be same as:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70942
Comment 3 Paolo Carlini 2017-10-04 09:20:43 UTC
Dup.

*** This bug has been marked as a duplicate of bug 71117 ***