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?
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; }; ^ /////////////////////////////
Actually, I'm no longer sure that `a` *is* odr-used...
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.
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; ^
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?
I ran it with this thing: http://melpon.org/wandbox/permlink/gAsh89NaSSFspYjq
I know that web page, but today, 20150131, I can't reproduce the issue with the current tree on my machine.
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.
I can't see any warnings with my trunk build from today, either.
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.