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;








7















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.










share|improve this question






















  • 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












  • 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


















7















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.










share|improve this question






















  • 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












  • 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














7












7








7


2






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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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












  • Weird... Changing the lambda capture of cb3 to &test seems 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











  • 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












  • Weird... Changing the lambda capture of cb3 to &test seems 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













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
);



);













draft saved

draft discarded


















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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해