public inbox for overseers@sourceware.org
 help / color / mirror / Atom feed
* GCC Bug (tried to create an account)
@ 2017-01-18 12:21 Scala Hater
  0 siblings, 0 replies; only message in thread
From: Scala Hater @ 2017-01-18 12:21 UTC (permalink / raw)
  To: overseers

Hello, I send an email to you because I tried to create an account in order to 
file a bug.

Given the following code:

> #include <iostream>
> 
> int a = 12;
> 
> int main()
> {
> 
>     auto lambda = [&](int i) -> void {
>     
>         std::cout << a << " " << i << std::endl;
>         a = i;
>         std::cout << a << std::endl;
>     
>     };
>     void (*f)(int) = lambda; // This should explode in your face
>     f(5);
>     std::cout << a << std::endl;
>     return 0;
> 
> }

In the line:

> void (*f)(int) = lambda;

We are assigning a lambda to a function pointer. According to the 
documentation here: http://en.cppreference.com/w/cpp/language/lambda It is 
stated that:

>This user-defined conversion function is only defined if the capture list of 
the lambda-expression is empty

This should give an error whenever the lambda is declared with a capture list.

On other compilers like Clang, MSVC, etc... when lambdas are declared with a 
capture list and this exact same situation, compilation fails. In GCC it only 
fails when a variable is actually captured (by using it inside the lambda). 
See the following links for comparison:

gcc: http://cpp.sh/9tphj
clang: https://godbolt.org/g/N88pbH

Please note that in the example the variable a is global, so even without a 
capture, this variable can be used inside the function. On the other hand when 
a is a local variable, compilation correctly fails.

Thanks.

Marc & Adrià from Catalonia.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-01-18 12:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-18 12:21 GCC Bug (tried to create an account) Scala Hater

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).