From: Scala Hater <scalahater@gmail.com>
To: overseers@gcc.gnu.org
Subject: GCC Bug (tried to create an account)
Date: Wed, 18 Jan 2017 12:21:00 -0000 [thread overview]
Message-ID: <4200972.xYN7sdBr4C@debian> (raw)
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.
reply other threads:[~2017-01-18 12:21 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4200972.xYN7sdBr4C@debian \
--to=scalahater@gmail.com \
--cc=overseers@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).