Lambda copying a reference to a lambda reference VS2017 compile errorWhat is a lambda (function)?What are the differences between a pointer variable and a reference variable in C++?What is the difference between a 'closure' and a 'lambda'?Why are Python lambdas useful?Distinct() with lambda?list comprehension vs. lambda + filterWhat is the copy-and-swap idiom?What is a lambda expression in C++11?What is an undefined reference/unresolved external symbol error and how do I fix it?Compiling an application for use in highly radioactive environments
Is Grandpa Irrational? Another Grandpa Mystery
Why did NASA use Imperial units?
USA: Can a witness take the 5th to avoid perjury?
What is the purpose of the fuel shutoff valve?
Why did modems have speakers?
Symbol of the tennis ball
I can't understand how 'static' works exactly
Why is chess failing to attract big name sponsors?
Why are angular mometum and angular velocity not necessarily parallel, but linear momentum and linear velocity are always parallel?
Direct revelation mechanism's sets of strategies and types
Grid/table with lots of buttons
How to repeat the last : command for a visual selection instead of whole buffer?
What is the meaning of "you has the wind of me"?
Do Rabbis get punished in Heaven for wrong interpretations or claims?
If my business card says 〇〇さん, does that mean I'm referring to myself with an honourific?
Spoken encryption
How can I receive packages while in France?
Why are line integrals not always path independent?
Impact of throwing away fruit waste on a peak > 3200 m above a glacier
What is the meaning of "a thinly disguised price"?
Why is a dedicated QA team member necessary?
401(k) investment after being fired. Do I own it?
Distribute large Tikz image on two A4 landscape
Do male skunks have teats?
Lambda copying a reference to a lambda reference VS2017 compile error
What is a lambda (function)?What are the differences between a pointer variable and a reference variable in C++?What is the difference between a 'closure' and a 'lambda'?Why are Python lambdas useful?Distinct() with lambda?list comprehension vs. lambda + filterWhat is the copy-and-swap idiom?What is a lambda expression in C++11?What is an undefined reference/unresolved external symbol error and how do I fix it?Compiling an application for use in highly radioactive environments
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Compiling the following code in Visual Studio 2017:
#include "pch.h"
#include <iostream>
int main()
int test = 5;
auto cb1 = [test]()
auto cb2 = [&]()
auto cb3 = [test]()
std::cout << test;
;
cb3();
;
cb2();
;
cb1();
Gives the compiler error
test.cpp(17): error C2440: '<function-style-cast>': cannot convert from 'const int' to 'main::<lambda_80fd0d4feae1377a5d8b8955e10105ab>::()::<lambda_38fc83ae6a7bd6540ebe1721869db4f1>'
test.cpp(17): note: No constructor could take the source type, or constructor overload resolution was ambiguous
test.cpp(18): error C3536: 'cb2': cannot be used before it is initialized
test.cpp(18): error C2064: term does not evaluate to a function taking 0 arguments
Does anybody know why Visual Studio gives this error? (it seem to compile ok on clang) You can get it to compile by replacing auto cb2 = [&]() with auto cb2 = [&test]() why does that fix the errors?
Even more interesting adding std::cout << test; or const int &ref = test; to the body of cb2 fixes the compiler error.
c++ lambda
add a comment |
Compiling the following code in Visual Studio 2017:
#include "pch.h"
#include <iostream>
int main()
int test = 5;
auto cb1 = [test]()
auto cb2 = [&]()
auto cb3 = [test]()
std::cout << test;
;
cb3();
;
cb2();
;
cb1();
Gives the compiler error
test.cpp(17): error C2440: '<function-style-cast>': cannot convert from 'const int' to 'main::<lambda_80fd0d4feae1377a5d8b8955e10105ab>::()::<lambda_38fc83ae6a7bd6540ebe1721869db4f1>'
test.cpp(17): note: No constructor could take the source type, or constructor overload resolution was ambiguous
test.cpp(18): error C3536: 'cb2': cannot be used before it is initialized
test.cpp(18): error C2064: term does not evaluate to a function taking 0 arguments
Does anybody know why Visual Studio gives this error? (it seem to compile ok on clang) You can get it to compile by replacing auto cb2 = [&]() with auto cb2 = [&test]() why does that fix the errors?
Even more interesting adding std::cout << test; or const int &ref = test; to the body of cb2 fixes the compiler error.
c++ lambda
Due to the fact, that G++ accepts such code, I suspect that it's a bug in VS2017, unless it's some form of UB, that I don't know about.
– Algirdas Preidžius
Mar 26 at 15:33
Looks like MSVS isn't treating the treating the use oftestin the capture ofcb3as a valid reason to capturetestincb2. Not sure who is right in this case but I'm inclined to think the gcc is behaving correctly.
– NathanOliver
Mar 26 at 15:33
I can reproduce this. My guess would be that the parser is confused about what[&]captures - it apparently tries to capture the implicitthis(thecb1instance thatcb2is in) on the one hand, but tries to initialize it withtestdirectly. All your fixes probably alleviate that confusion. But in the end it's a bug in MSVC either way.
– Max Langhof
Mar 26 at 15:39
Weird... Changing the lambda capture ofcb3to&testseems to do the job. rextester.com/FEFQ33220
– Constantinos Glynos
Mar 26 at 15:57
add a comment |
Compiling the following code in Visual Studio 2017:
#include "pch.h"
#include <iostream>
int main()
int test = 5;
auto cb1 = [test]()
auto cb2 = [&]()
auto cb3 = [test]()
std::cout << test;
;
cb3();
;
cb2();
;
cb1();
Gives the compiler error
test.cpp(17): error C2440: '<function-style-cast>': cannot convert from 'const int' to 'main::<lambda_80fd0d4feae1377a5d8b8955e10105ab>::()::<lambda_38fc83ae6a7bd6540ebe1721869db4f1>'
test.cpp(17): note: No constructor could take the source type, or constructor overload resolution was ambiguous
test.cpp(18): error C3536: 'cb2': cannot be used before it is initialized
test.cpp(18): error C2064: term does not evaluate to a function taking 0 arguments
Does anybody know why Visual Studio gives this error? (it seem to compile ok on clang) You can get it to compile by replacing auto cb2 = [&]() with auto cb2 = [&test]() why does that fix the errors?
Even more interesting adding std::cout << test; or const int &ref = test; to the body of cb2 fixes the compiler error.
c++ lambda
Compiling the following code in Visual Studio 2017:
#include "pch.h"
#include <iostream>
int main()
int test = 5;
auto cb1 = [test]()
auto cb2 = [&]()
auto cb3 = [test]()
std::cout << test;
;
cb3();
;
cb2();
;
cb1();
Gives the compiler error
test.cpp(17): error C2440: '<function-style-cast>': cannot convert from 'const int' to 'main::<lambda_80fd0d4feae1377a5d8b8955e10105ab>::()::<lambda_38fc83ae6a7bd6540ebe1721869db4f1>'
test.cpp(17): note: No constructor could take the source type, or constructor overload resolution was ambiguous
test.cpp(18): error C3536: 'cb2': cannot be used before it is initialized
test.cpp(18): error C2064: term does not evaluate to a function taking 0 arguments
Does anybody know why Visual Studio gives this error? (it seem to compile ok on clang) You can get it to compile by replacing auto cb2 = [&]() with auto cb2 = [&test]() why does that fix the errors?
Even more interesting adding std::cout << test; or const int &ref = test; to the body of cb2 fixes the compiler error.
c++ lambda
c++ lambda
asked Mar 26 at 15:23
David FooksDavid Fooks
6688 silver badges10 bronze badges
6688 silver badges10 bronze badges
Due to the fact, that G++ accepts such code, I suspect that it's a bug in VS2017, unless it's some form of UB, that I don't know about.
– Algirdas Preidžius
Mar 26 at 15:33
Looks like MSVS isn't treating the treating the use oftestin the capture ofcb3as a valid reason to capturetestincb2. Not sure who is right in this case but I'm inclined to think the gcc is behaving correctly.
– NathanOliver
Mar 26 at 15:33
I can reproduce this. My guess would be that the parser is confused about what[&]captures - it apparently tries to capture the implicitthis(thecb1instance thatcb2is in) on the one hand, but tries to initialize it withtestdirectly. All your fixes probably alleviate that confusion. But in the end it's a bug in MSVC either way.
– Max Langhof
Mar 26 at 15:39
Weird... Changing the lambda capture ofcb3to&testseems to do the job. rextester.com/FEFQ33220
– Constantinos Glynos
Mar 26 at 15:57
add a comment |
Due to the fact, that G++ accepts such code, I suspect that it's a bug in VS2017, unless it's some form of UB, that I don't know about.
– Algirdas Preidžius
Mar 26 at 15:33
Looks like MSVS isn't treating the treating the use oftestin the capture ofcb3as a valid reason to capturetestincb2. Not sure who is right in this case but I'm inclined to think the gcc is behaving correctly.
– NathanOliver
Mar 26 at 15:33
I can reproduce this. My guess would be that the parser is confused about what[&]captures - it apparently tries to capture the implicitthis(thecb1instance thatcb2is in) on the one hand, but tries to initialize it withtestdirectly. All your fixes probably alleviate that confusion. But in the end it's a bug in MSVC either way.
– Max Langhof
Mar 26 at 15:39
Weird... Changing the lambda capture ofcb3to&testseems to do the job. rextester.com/FEFQ33220
– Constantinos Glynos
Mar 26 at 15:57
Due to the fact, that G++ accepts such code, I suspect that it's a bug in VS2017, unless it's some form of UB, that I don't know about.
– Algirdas Preidžius
Mar 26 at 15:33
Due to the fact, that G++ accepts such code, I suspect that it's a bug in VS2017, unless it's some form of UB, that I don't know about.
– Algirdas Preidžius
Mar 26 at 15:33
Looks like MSVS isn't treating the treating the use of
test in the capture of cb3 as a valid reason to capture test incb2. Not sure who is right in this case but I'm inclined to think the gcc is behaving correctly.– NathanOliver
Mar 26 at 15:33
Looks like MSVS isn't treating the treating the use of
test in the capture of cb3 as a valid reason to capture test incb2. Not sure who is right in this case but I'm inclined to think the gcc is behaving correctly.– NathanOliver
Mar 26 at 15:33
I can reproduce this. My guess would be that the parser is confused about what
[&] captures - it apparently tries to capture the implicit this (the cb1 instance that cb2 is in) on the one hand, but tries to initialize it with test directly. All your fixes probably alleviate that confusion. But in the end it's a bug in MSVC either way.– Max Langhof
Mar 26 at 15:39
I can reproduce this. My guess would be that the parser is confused about what
[&] captures - it apparently tries to capture the implicit this (the cb1 instance that cb2 is in) on the one hand, but tries to initialize it with test directly. All your fixes probably alleviate that confusion. But in the end it's a bug in MSVC either way.– Max Langhof
Mar 26 at 15:39
Weird... Changing the lambda capture of
cb3 to &test seems to do the job. rextester.com/FEFQ33220– Constantinos Glynos
Mar 26 at 15:57
Weird... Changing the lambda capture of
cb3 to &test seems to do the job. rextester.com/FEFQ33220– Constantinos Glynos
Mar 26 at 15:57
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55360734%2flambda-copying-a-reference-to-a-lambda-reference-vs2017-compile-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55360734%2flambda-copying-a-reference-to-a-lambda-reference-vs2017-compile-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Due to the fact, that G++ accepts such code, I suspect that it's a bug in VS2017, unless it's some form of UB, that I don't know about.
– Algirdas Preidžius
Mar 26 at 15:33
Looks like MSVS isn't treating the treating the use of
testin the capture ofcb3as a valid reason to capturetestincb2. Not sure who is right in this case but I'm inclined to think the gcc is behaving correctly.– NathanOliver
Mar 26 at 15:33
I can reproduce this. My guess would be that the parser is confused about what
[&]captures - it apparently tries to capture the implicitthis(thecb1instance thatcb2is in) on the one hand, but tries to initialize it withtestdirectly. All your fixes probably alleviate that confusion. But in the end it's a bug in MSVC either way.– Max Langhof
Mar 26 at 15:39
Weird... Changing the lambda capture of
cb3to&testseems to do the job. rextester.com/FEFQ33220– Constantinos Glynos
Mar 26 at 15:57