Is it possible to create a lambda on the heap in one step? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
21
down vote
favorite
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?
c++ c++17
marked as duplicate by NathanOliver
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.
add a comment |Â
up vote
21
down vote
favorite
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?
c++ c++17
marked as duplicate by NathanOliver
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? sinceauto
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
add a comment |Â
up vote
21
down vote
favorite
up vote
21
down vote
favorite
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?
c++ c++17
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
c++ c++17
asked Aug 14 at 10:50
geza
9,19322360
9,19322360
marked as duplicate by NathanOliver
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
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? sinceauto
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
add a comment |Â
I think the question should be: what is the real type of lambda? sinceauto
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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
36
down vote
accepted
You can use auto
in a new-expression:
new auto (());
More interesting question - is it possible to create shared lambda usingmake_shared
? ;)
â bartop
Aug 14 at 11:37
yes it is, here ismake_unique
example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
â Marek R
Aug 14 at 11:38
@MarekR that's not exactly lambda on heap though. You createunique_ptr
rostd::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
add a comment |Â
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 (());
More interesting question - is it possible to create shared lambda usingmake_shared
? ;)
â bartop
Aug 14 at 11:37
yes it is, here ismake_unique
example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
â Marek R
Aug 14 at 11:38
@MarekR that's not exactly lambda on heap though. You createunique_ptr
rostd::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
add a comment |Â
up vote
36
down vote
accepted
You can use auto
in a new-expression:
new auto (());
More interesting question - is it possible to create shared lambda usingmake_shared
? ;)
â bartop
Aug 14 at 11:37
yes it is, here ismake_unique
example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
â Marek R
Aug 14 at 11:38
@MarekR that's not exactly lambda on heap though. You createunique_ptr
rostd::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
add a comment |Â
up vote
36
down vote
accepted
up vote
36
down vote
accepted
You can use auto
in a new-expression:
new auto (());
You can use auto
in a new-expression:
new auto (());
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 usingmake_shared
? ;)
â bartop
Aug 14 at 11:37
yes it is, here ismake_unique
example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
â Marek R
Aug 14 at 11:38
@MarekR that's not exactly lambda on heap though. You createunique_ptr
rostd::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
add a comment |Â
More interesting question - is it possible to create shared lambda usingmake_shared
? ;)
â bartop
Aug 14 at 11:37
yes it is, here ismake_unique
example: wandbox.org/permlink/pR7FWQfo0SSHAKhH
â Marek R
Aug 14 at 11:38
@MarekR that's not exactly lambda on heap though. You createunique_ptr
rostd::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
add a comment |Â
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