[Bug c++/78816] New: [c++14] Static auto variable usage in generic/variadic lambda fails to compile

jeroendebusser at telenet dot be gcc-bugzilla@gcc.gnu.org
Wed Dec 14 18:20:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78816

            Bug ID: 78816
           Summary: [c++14] Static auto variable usage in generic/variadic
                    lambda fails to compile
           Product: gcc
           Version: 6.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeroendebusser at telenet dot be
  Target Milestone: ---

Sample code adapted from https://adishavit.github.io/2016/Technical-Debt/
(https://godbolt.org/g/osKPbq):
void f(void (*f1)(int)) {
  f1(42);
}

//Converts any lambda to a captureless lambda that converts nicely to a
function pointer for the lifetime of the temporary.
template <typename Lambda>
static auto callback(Lambda &&l)
{
  static auto* p = &l;
  p = &l;
  return [](auto... x){ return (*p)(x...); };
}

int main() {
  int x = 5;
  f(callback([=](int y){std::cout << x << ' ' << y;}));
}

Gives the error:
<source>: In instantiation of 'callback(Lambda&&)::<lambda(auto:1 ...)> [with
auto:1 = {int}; Lambda = main()::<lambda(int)>]':
<source>:14:22:   required by substitution of 'template<class ... auto:1>
callback(Lambda&&) [with Lambda = main()::<lambda(int)>]::<lambda(auto:1
...)>::operator decltype (((const callback(Lambda&&) [with Lambda =
main()::<lambda(int)>]::<lambda(auto:1 ...)>*)((const callback(Lambda&&) [with
Lambda = main()::<lambda(int)>]::<lambda(auto:1 ...)>*
const)0u))->operator()(static_cast<auto:1&>(callback::__lambda0<main::__lambda1>::_FUN::<unnamed>)
...)) (*)(auto:1 ...)() const [with auto:1 = {int}]'
<source>:19:54:   required from here
<source>:14:33: error: use of 'p' before deduction of 'auto'
return [](auto... x){ return (*p)(x...); };
~^~~
<source>:14:36: error: invalid use of 'auto'
return [](auto... x){ return (*p)(x...); };
~~~~^~~~~~

Making the lambda in callback non-generic(ie: [](int x){return (*p)(x);}) or
explicitly calculating the type of p(std::add_pointer_t<decltype(l)>) makes the
code compile successfully.


More information about the Gcc-bugs mailing list