Is it possible to create a lambda on the heap in one step? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
21
down vote

favorite
4













This question already has an answer here:



  • Lambda with dynamic storage duration

    4 answers



We can create a lambda like this:



auto x = ();


I can create a copy of this on the heap like this:



auto y = new decltype(x)(x);


The question is, is it possible to do this in one step? Creating a lambda on the heap without extra steps?







share|improve this question












marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 14 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • I think the question should be: what is the real type of lambda? since auto just hide the type name.
    – SHR
    Aug 14 at 10:56






  • 2




    @SHR The lambda type is unnamed: "The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type". Its "real type" is usually some compiler-generated gibberish name but that doesn't help you because you cannot directly name it anyway.
    – Max Langhof
    Aug 14 at 10:57







  • 7




    answer shows that it is possible, but the real question is; WHY?
    – Marek R
    Aug 14 at 11:30














up vote
21
down vote

favorite
4













This question already has an answer here:



  • Lambda with dynamic storage duration

    4 answers



We can create a lambda like this:



auto x = ();


I can create a copy of this on the heap like this:



auto y = new decltype(x)(x);


The question is, is it possible to do this in one step? Creating a lambda on the heap without extra steps?







share|improve this question












marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 14 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • I think the question should be: what is the real type of lambda? since auto just hide the type name.
    – SHR
    Aug 14 at 10:56






  • 2




    @SHR The lambda type is unnamed: "The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type". Its "real type" is usually some compiler-generated gibberish name but that doesn't help you because you cannot directly name it anyway.
    – Max Langhof
    Aug 14 at 10:57







  • 7




    answer shows that it is possible, but the real question is; WHY?
    – Marek R
    Aug 14 at 11:30












up vote
21
down vote

favorite
4









up vote
21
down vote

favorite
4






4






This question already has an answer here:



  • Lambda with dynamic storage duration

    4 answers



We can create a lambda like this:



auto x = ();


I can create a copy of this on the heap like this:



auto y = new decltype(x)(x);


The question is, is it possible to do this in one step? Creating a lambda on the heap without extra steps?







share|improve this question













This question already has an answer here:



  • Lambda with dynamic storage duration

    4 answers



We can create a lambda like this:



auto x = ();


I can create a copy of this on the heap like this:



auto y = new decltype(x)(x);


The question is, is it possible to do this in one step? Creating a lambda on the heap without extra steps?





This question already has an answer here:



  • Lambda with dynamic storage duration

    4 answers









share|improve this question











share|improve this question




share|improve this question










asked Aug 14 at 10:50









geza

9,19322360




9,19322360




marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 14 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by NathanOliver c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Aug 14 at 13:14


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • I think the question should be: what is the real type of lambda? since auto just hide the type name.
    – SHR
    Aug 14 at 10:56






  • 2




    @SHR The lambda type is unnamed: "The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type". Its "real type" is usually some compiler-generated gibberish name but that doesn't help you because you cannot directly name it anyway.
    – Max Langhof
    Aug 14 at 10:57







  • 7




    answer shows that it is possible, but the real question is; WHY?
    – Marek R
    Aug 14 at 11:30
















  • I think the question should be: what is the real type of lambda? since auto just hide the type name.
    – SHR
    Aug 14 at 10:56






  • 2




    @SHR The lambda type is unnamed: "The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type". Its "real type" is usually some compiler-generated gibberish name but that doesn't help you because you cannot directly name it anyway.
    – Max Langhof
    Aug 14 at 10:57







  • 7




    answer shows that it is possible, but the real question is; WHY?
    – Marek R
    Aug 14 at 11:30















I think the question should be: what is the real type of lambda? since auto just hide the type name.
– SHR
Aug 14 at 10:56




I think the question should be: what is the real type of lambda? since auto just hide the type name.
– SHR
Aug 14 at 10:56




2




2




@SHR The lambda type is unnamed: "The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type". Its "real type" is usually some compiler-generated gibberish name but that doesn't help you because you cannot directly name it anyway.
– Max Langhof
Aug 14 at 10:57





@SHR The lambda type is unnamed: "The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type". Its "real type" is usually some compiler-generated gibberish name but that doesn't help you because you cannot directly name it anyway.
– Max Langhof
Aug 14 at 10:57





7




7




answer shows that it is possible, but the real question is; WHY?
– Marek R
Aug 14 at 11:30




answer shows that it is possible, but the real question is; WHY?
– Marek R
Aug 14 at 11:30












1 Answer
1






active

oldest

votes

















up vote
36
down vote



accepted










You can use auto in a new-expression:



new auto (());





share|improve this answer






















  • More interesting question - is it possible to create shared lambda using make_shared? ;)
    – bartop
    Aug 14 at 11:37










  • yes it is, here is make_unique example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
    – Marek R
    Aug 14 at 11:38










  • @MarekR that's not exactly lambda on heap though. You create unique_ptr ro std::function which is init-ed with lambda, not unique_ptr to lambda.
    – Dan M.
    Aug 14 at 11:44






  • 1




    @DanM. wandbox.org/permlink/RFX021jIRf7vBFzh
    – Marek R
    Aug 14 at 11:54

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
36
down vote



accepted










You can use auto in a new-expression:



new auto (());





share|improve this answer






















  • More interesting question - is it possible to create shared lambda using make_shared? ;)
    – bartop
    Aug 14 at 11:37










  • yes it is, here is make_unique example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
    – Marek R
    Aug 14 at 11:38










  • @MarekR that's not exactly lambda on heap though. You create unique_ptr ro std::function which is init-ed with lambda, not unique_ptr to lambda.
    – Dan M.
    Aug 14 at 11:44






  • 1




    @DanM. wandbox.org/permlink/RFX021jIRf7vBFzh
    – Marek R
    Aug 14 at 11:54














up vote
36
down vote



accepted










You can use auto in a new-expression:



new auto (());





share|improve this answer






















  • More interesting question - is it possible to create shared lambda using make_shared? ;)
    – bartop
    Aug 14 at 11:37










  • yes it is, here is make_unique example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
    – Marek R
    Aug 14 at 11:38










  • @MarekR that's not exactly lambda on heap though. You create unique_ptr ro std::function which is init-ed with lambda, not unique_ptr to lambda.
    – Dan M.
    Aug 14 at 11:44






  • 1




    @DanM. wandbox.org/permlink/RFX021jIRf7vBFzh
    – Marek R
    Aug 14 at 11:54












up vote
36
down vote



accepted







up vote
36
down vote



accepted






You can use auto in a new-expression:



new auto (());





share|improve this answer














You can use auto in a new-expression:



new auto (());






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 14 at 11:00

























answered Aug 14 at 10:53









Oliv

6,4031747




6,4031747











  • More interesting question - is it possible to create shared lambda using make_shared? ;)
    – bartop
    Aug 14 at 11:37










  • yes it is, here is make_unique example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
    – Marek R
    Aug 14 at 11:38










  • @MarekR that's not exactly lambda on heap though. You create unique_ptr ro std::function which is init-ed with lambda, not unique_ptr to lambda.
    – Dan M.
    Aug 14 at 11:44






  • 1




    @DanM. wandbox.org/permlink/RFX021jIRf7vBFzh
    – Marek R
    Aug 14 at 11:54
















  • More interesting question - is it possible to create shared lambda using make_shared? ;)
    – bartop
    Aug 14 at 11:37










  • yes it is, here is make_unique example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
    – Marek R
    Aug 14 at 11:38










  • @MarekR that's not exactly lambda on heap though. You create unique_ptr ro std::function which is init-ed with lambda, not unique_ptr to lambda.
    – Dan M.
    Aug 14 at 11:44






  • 1




    @DanM. wandbox.org/permlink/RFX021jIRf7vBFzh
    – Marek R
    Aug 14 at 11:54















More interesting question - is it possible to create shared lambda using make_shared? ;)
– bartop
Aug 14 at 11:37




More interesting question - is it possible to create shared lambda using make_shared? ;)
– bartop
Aug 14 at 11:37












yes it is, here is make_unique example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
– Marek R
Aug 14 at 11:38




yes it is, here is make_unique example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
– Marek R
Aug 14 at 11:38












@MarekR that's not exactly lambda on heap though. You create unique_ptr ro std::function which is init-ed with lambda, not unique_ptr to lambda.
– Dan M.
Aug 14 at 11:44




@MarekR that's not exactly lambda on heap though. You create unique_ptr ro std::function which is init-ed with lambda, not unique_ptr to lambda.
– Dan M.
Aug 14 at 11:44




1




1




@DanM. wandbox.org/permlink/RFX021jIRf7vBFzh
– Marek R
Aug 14 at 11:54




@DanM. wandbox.org/permlink/RFX021jIRf7vBFzh
– Marek R
Aug 14 at 11:54


這個網誌中的熱門文章

How to combine Bézier curves to a surface?

Mutual Information Always Non-negative

Why am i infinitely getting the same tweet with the Twitter Search API?