Bug 64791 - Generic lambda fails to implicitly capture `const` variable
Summary: Generic lambda fails to implicitly capture `const` variable
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.2
: P3 normal
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2015-01-25 21:33 UTC by Tomalak Geret'kal
Modified: 2015-02-02 16:00 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 5.0
Known to fail: 4.8.2, 4.9.1
Last reconfirmed: 2015-01-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tomalak Geret'kal 2015-01-25 21:33:17 UTC
From http://stackoverflow.com/q/28141403/560648

Reproduction:

/////////////////////////////
#include <iostream>
#include <functional>

int main()
{
    const int a = 2;
    std::function<void(int)> f = [&](auto b) { std::cout << a << ", " << b << std::endl; };
    f(3);
}
/////////////////////////////


Taking any of the following steps allows the program to build and run with the expected output "2, 3":

- remove `const` from declaration of a
- name `a` in the capture-list instead of relying on implicit capture
- change declaration of `f` from `std::function<void(int)>` to `auto`
- make the lambda non-generic by changing `auto b` to `int b`
- use Clang (e.g. v3.5.0)

Suspect detection of odr-use is breaking, or this could be related to bug 61814, or something else entirely?
Comment 1 Tomalak Geret'kal 2015-01-25 21:34:17 UTC
Build error:

/////////////////////////////
main.cpp: In instantiation of 'main()::<lambda(auto:1)> [with auto:1 = int]':
/usr/local/include/c++/4.9.2/functional:2149:71:   required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Functor> using _Invoke = decltype (std::__callable_functor(declval<_Functor&>())((declval<_ArgTypes>)()...)) [with _Functor = main()::<lambda(auto:1)> _Res = void; _ArgTypes = {int}]'
/usr/local/include/c++/4.9.2/functional:2225:9:   required by substitution of 'template<class _Functor, class> std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = main()::<lambda(auto:1)> <template-parameter-1-2> = <missing>]'
main.cpp:7:90:   required from here
main.cpp:7:58: error: 'a' was not declared in this scope
     std::function<void(int)> f = [&](auto b) { std::cout << a << ", " << b << std::endl; };
                                                          ^
/////////////////////////////
Comment 2 Tomalak Geret'kal 2015-01-25 22:13:49 UTC
Actually, I'm no longer sure that `a` *is* odr-used...
Comment 3 Ville Voutilainen 2015-01-27 12:48:24 UTC
Trunk accepts the code and prints 2, 3 like clang does. So unless there's a need for an additional testcase or a backport, this looks like a candidate for closing.
Comment 4 Ville Voutilainen 2015-01-27 12:53:22 UTC
Oh, wait a minute, with trunk, I see an incorrect warning:

prog.cc: In function 'int main()':
prog.cc:7:15: warning: variable 'a' set but not used [-Wunused-but-set-variable]
    const int a = 2;
              ^
Comment 5 Paolo Carlini 2015-01-31 11:52:12 UTC
Hi ville. With r220303 I don't see the warning, at any optimization level and various combinations of other flags + -Wunused-but-set-variable of course. Can you please double check / provide more information?
Comment 6 Ville Voutilainen 2015-01-31 11:56:15 UTC
I ran it with this thing:
http://melpon.org/wandbox/permlink/gAsh89NaSSFspYjq
Comment 7 Paolo Carlini 2015-01-31 12:18:52 UTC
I know that web page, but today, 20150131, I can't reproduce the issue with the current tree on my machine.
Comment 8 Paolo Carlini 2015-01-31 16:14:48 UTC
Weird, I tried on my machine yesterday's r220267 and same result, no warning (with -std=gnu++1z -Wall -Wextra). I guess that before closing this bug we need either to figure out what's special about that web page or another report that things are actually fine.
Comment 9 Ville Voutilainen 2015-02-02 13:54:36 UTC
I can't see any warnings with my trunk build from today, either.
Comment 10 Paolo Carlini 2015-02-02 16:00:40 UTC
Ok, thanks Ville, let's tentatively close this as fixed for 5.0. I'll monitor it for a while anyway, probably I will also add something to the testsuite.