Different type in lamda with reference in capture list

Different type in lamda with reference in capture list



It is fine to compile these code in c11


double(*p1)(double) = (double a) return sqrt(a); ;

double(*p2)(double) = [&](double a) return sqrt(a); ;



But there is the following error in compilation in c17



double(*p2)(double) = [&](double a) return sqrt(a); ;



cannot convert test_pointer_to_function::test_method()::<lambda(double)> to double (*)(double) in initialization double(*p2)(double) = [&](double a) return sqrt(a); ;



What is the correct type for this lamda function?






Welcome to Stack Overflow.Use formatting tools to make your post more readable. Use code blocking for code and log and error texts and bold and italics to highlight things

– Prateek
Sep 8 '18 at 2:36


code blocking






It would be better to use std::function<double(double)> p2 = ... or simply use auto p2 = ...

– wcochran
Sep 8 '18 at 2:58


std::function<double(double)> p2 = ...


auto p2 = ...






Firstly, it is C++11 and C++17, not "c11" and "c17". Secondly, what made you to conclude that "it is fine" in C++11? The second initialization is illegal in C++11 just at it is illegal in C++17.

– AnT
Sep 8 '18 at 3:08







Your error message looks strange. Are you sure you quoted the right error message? I don't see any test_pointer_to_function or test_method in the code you posted. Yet, they are mentioned in the error message.

– AnT
Sep 8 '18 at 3:18


test_pointer_to_function


test_method




2 Answers
2



There's no "correct" function pointer type for lambda expression with non-empty lambda-capture. Such closure objects are not convertible to ordinary function pointers.



If you want to be able to convert closure object to ordinary function pointer type, make sure you have nothing in the part of lambda expression. This applies to C++11 and later, including C++17.




Each lambda has its own unique type. Normally, if you need to assign a lambda to a variable, you use auto for its type. If you need a known type (e.g. you are going to store lambdas in a container), you can use std::function with the same signature as your lambda. In your example, this will be


auto


std::function


std::function<double(double)> p2 = [&](double a) return sqrt(a); ;



(but note that you generally lose some efficiency this way).



Only non-capturing lambdas, i.e. lambdas with empty , are convertible to a function pointer, like in your example:



double(*p1)(double) = (double a) return sqrt(a); ;



Your C++11 compiler should not have accepted


double(*p2)(double) = [&](double a) return sqrt(a); ;



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế