Bug 61814 - [c++1y] cannot use decltype on captured variable in generic lambda
Summary: [c++1y] cannot use decltype on captured variable in generic lambda
Status: RESOLVED DUPLICATE of bug 63628
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2014-07-15 15:44 UTC by tongari95
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 5.0
Known to fail:
Last reconfirmed: 2014-10-23 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tongari95 2014-07-15 15:44:13 UTC
The code below has a lambda that captures the outer params t, for forwarding matter, decltype on t has to be used.
```
    auto const pack = [](auto&&... t)
    {
        return [&](auto&& f)->decltype(auto)
        {
            return f(static_cast<decltype(t)>(t)...);
        };
    };
    
    int main(int argc, char** argv) {
        pack(1)([](int){});
        return 0;
    }
```
It works with clang 3.5, but g++ 4.9.0 complains:
error: 't' was not declared in this scope

The same also applies to `sizeof` or that kind of compile-time thing.
Comment 1 Alexander Dubov 2014-10-23 11:59:27 UTC
This problem still happens on 4.9.1.

For example, this example works:

int main(int argc, char **argv)
{
        int a{5};
        float z(0.5);
        [&a](float z_) -> void {
                decltype(a) b = a;
        }(z);
        return 0;
}

But this basic modification fails to compile (g++-4.9.1 -std=gnu++14):

int main(int argc, char **argv)
{
        int a{5};
        float z(0.5);
        [&a](auto z_) -> void {
                decltype(a) b = a;
        }(z);
        return 0;
}
Comment 2 Jonathan Wakely 2014-10-23 12:23:46 UTC
(In reply to Alexander Dubov from comment #1)
> But this basic modification fails to compile (g++-4.9.1 -std=gnu++14):
> 
> int main(int argc, char **argv)
> {
>         int a{5};
>         float z(0.5);
>         [&a](auto z_) -> void {
>                 decltype(a) b = a;
>         }(z);
>         return 0;
> }

That seems to be a different issue, and is fixed on trunk.
Comment 3 Jonathan Wakely 2014-10-23 12:31:54 UTC
Actually the original problem is fixed on trunk too (but gives a different error now), so maybe it is the same and can be reduced to:

int main()
{
  int a{5};
  [&a](auto z) -> void {
    decltype(a) b = a;
  }(1);
}

l.cc: In instantiation of ‘main()::<lambda(auto:1)> [with auto:1 = int]’:
l.cc:6:6:   required from here
l.cc:5:17: error: ‘a’ was not declared in this scope
     decltype(a) b = a;
                 ^

The error goes away if the lambda doesn't have a trailing-return-type, or if it's not a generic lambda.

Jason, is it worth fixing this on the branch? It only affects C++14 mode.
Comment 4 Jason Merrill 2015-12-16 18:23:23 UTC
Fixed.

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