Nested std::bind in lambda cannot be casted to std::function (from void*)
I have to cast a member method call (SystemUpdater::OnSystemUpdateCompletedHandler) to void* in order to access as argument in a static C function.
Snippet 1:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, std::bind(&SystemUpdater::OnSystemUpdateCompletedHandler, this, std::placeholders::_1));
Snippet 2:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, [&](int result)OnSystemUpdateCompletedHandler(result); );
The second argument of StartUpdate is casted and executed in my static C function:
(*((std::function<void(int)>*) userdata))(_result);
This cast and execution works with snippet 2 but NOT with snippet 1 (Signal: SIGSEGV (Segmentation fault)).
Why?
StartUpdate is declared as:
UpdateResult_t StartUpdate(const std::string &path,const std::function<void(int)> &finishCallback)
c++ lambda std-function stdbind
|
show 1 more comment
I have to cast a member method call (SystemUpdater::OnSystemUpdateCompletedHandler) to void* in order to access as argument in a static C function.
Snippet 1:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, std::bind(&SystemUpdater::OnSystemUpdateCompletedHandler, this, std::placeholders::_1));
Snippet 2:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, [&](int result)OnSystemUpdateCompletedHandler(result); );
The second argument of StartUpdate is casted and executed in my static C function:
(*((std::function<void(int)>*) userdata))(_result);
This cast and execution works with snippet 2 but NOT with snippet 1 (Signal: SIGSEGV (Segmentation fault)).
Why?
StartUpdate is declared as:
UpdateResult_t StartUpdate(const std::string &path,const std::function<void(int)> &finishCallback)
c++ lambda std-function stdbind
2
You know, that a lambda is not a pointer to anstd::function
and that your second snippet is probably only working because theoperator ()
ofstd::function
in the compiler you are using just happens to also work whenthis
is pointing to certain lambda types? Or is your code ofStartUpdate
doing something to get thatuserdata
to anstd::function
pointer?
– Yanick Salzmann
Nov 15 '18 at 13:32
1
Please be more specific than ”it doesn’t work”. And read about the Minimal, Complete, and Verifiable example.
– molbdnilo
Nov 15 '18 at 13:35
@JeJo theresult
is whats passed through thestd::placeholders::_1
to the bound function.
– Yanick Salzmann
Nov 15 '18 at 13:37
Thanks for your replys! @molbdnilo I know that my question is not that clear - thank you for the information. I will try to be more specific.
– Fabian Knapp
Nov 15 '18 at 13:46
@yanick-salzmann I have added the declaration of StartUpdate. I dont do any magic with userdata in StartUpdate.
– Fabian Knapp
Nov 15 '18 at 13:46
|
show 1 more comment
I have to cast a member method call (SystemUpdater::OnSystemUpdateCompletedHandler) to void* in order to access as argument in a static C function.
Snippet 1:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, std::bind(&SystemUpdater::OnSystemUpdateCompletedHandler, this, std::placeholders::_1));
Snippet 2:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, [&](int result)OnSystemUpdateCompletedHandler(result); );
The second argument of StartUpdate is casted and executed in my static C function:
(*((std::function<void(int)>*) userdata))(_result);
This cast and execution works with snippet 2 but NOT with snippet 1 (Signal: SIGSEGV (Segmentation fault)).
Why?
StartUpdate is declared as:
UpdateResult_t StartUpdate(const std::string &path,const std::function<void(int)> &finishCallback)
c++ lambda std-function stdbind
I have to cast a member method call (SystemUpdater::OnSystemUpdateCompletedHandler) to void* in order to access as argument in a static C function.
Snippet 1:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, std::bind(&SystemUpdater::OnSystemUpdateCompletedHandler, this, std::placeholders::_1));
Snippet 2:
updateDownloader->StartDownloadAsync(_updateRequest.url, "update-test", [&](std::string resultFile, Network::FileDownloaderResult_t result) {
this->updateHelper->StartUpdate(resultFile, [&](int result)OnSystemUpdateCompletedHandler(result); );
The second argument of StartUpdate is casted and executed in my static C function:
(*((std::function<void(int)>*) userdata))(_result);
This cast and execution works with snippet 2 but NOT with snippet 1 (Signal: SIGSEGV (Segmentation fault)).
Why?
StartUpdate is declared as:
UpdateResult_t StartUpdate(const std::string &path,const std::function<void(int)> &finishCallback)
c++ lambda std-function stdbind
c++ lambda std-function stdbind
edited Nov 15 '18 at 13:42
Fabian Knapp
asked Nov 15 '18 at 13:21
Fabian KnappFabian Knapp
1,1661020
1,1661020
2
You know, that a lambda is not a pointer to anstd::function
and that your second snippet is probably only working because theoperator ()
ofstd::function
in the compiler you are using just happens to also work whenthis
is pointing to certain lambda types? Or is your code ofStartUpdate
doing something to get thatuserdata
to anstd::function
pointer?
– Yanick Salzmann
Nov 15 '18 at 13:32
1
Please be more specific than ”it doesn’t work”. And read about the Minimal, Complete, and Verifiable example.
– molbdnilo
Nov 15 '18 at 13:35
@JeJo theresult
is whats passed through thestd::placeholders::_1
to the bound function.
– Yanick Salzmann
Nov 15 '18 at 13:37
Thanks for your replys! @molbdnilo I know that my question is not that clear - thank you for the information. I will try to be more specific.
– Fabian Knapp
Nov 15 '18 at 13:46
@yanick-salzmann I have added the declaration of StartUpdate. I dont do any magic with userdata in StartUpdate.
– Fabian Knapp
Nov 15 '18 at 13:46
|
show 1 more comment
2
You know, that a lambda is not a pointer to anstd::function
and that your second snippet is probably only working because theoperator ()
ofstd::function
in the compiler you are using just happens to also work whenthis
is pointing to certain lambda types? Or is your code ofStartUpdate
doing something to get thatuserdata
to anstd::function
pointer?
– Yanick Salzmann
Nov 15 '18 at 13:32
1
Please be more specific than ”it doesn’t work”. And read about the Minimal, Complete, and Verifiable example.
– molbdnilo
Nov 15 '18 at 13:35
@JeJo theresult
is whats passed through thestd::placeholders::_1
to the bound function.
– Yanick Salzmann
Nov 15 '18 at 13:37
Thanks for your replys! @molbdnilo I know that my question is not that clear - thank you for the information. I will try to be more specific.
– Fabian Knapp
Nov 15 '18 at 13:46
@yanick-salzmann I have added the declaration of StartUpdate. I dont do any magic with userdata in StartUpdate.
– Fabian Knapp
Nov 15 '18 at 13:46
2
2
You know, that a lambda is not a pointer to an
std::function
and that your second snippet is probably only working because the operator ()
of std::function
in the compiler you are using just happens to also work when this
is pointing to certain lambda types? Or is your code of StartUpdate
doing something to get that userdata
to an std::function
pointer?– Yanick Salzmann
Nov 15 '18 at 13:32
You know, that a lambda is not a pointer to an
std::function
and that your second snippet is probably only working because the operator ()
of std::function
in the compiler you are using just happens to also work when this
is pointing to certain lambda types? Or is your code of StartUpdate
doing something to get that userdata
to an std::function
pointer?– Yanick Salzmann
Nov 15 '18 at 13:32
1
1
Please be more specific than ”it doesn’t work”. And read about the Minimal, Complete, and Verifiable example.
– molbdnilo
Nov 15 '18 at 13:35
Please be more specific than ”it doesn’t work”. And read about the Minimal, Complete, and Verifiable example.
– molbdnilo
Nov 15 '18 at 13:35
@JeJo the
result
is whats passed through the std::placeholders::_1
to the bound function.– Yanick Salzmann
Nov 15 '18 at 13:37
@JeJo the
result
is whats passed through the std::placeholders::_1
to the bound function.– Yanick Salzmann
Nov 15 '18 at 13:37
Thanks for your replys! @molbdnilo I know that my question is not that clear - thank you for the information. I will try to be more specific.
– Fabian Knapp
Nov 15 '18 at 13:46
Thanks for your replys! @molbdnilo I know that my question is not that clear - thank you for the information. I will try to be more specific.
– Fabian Knapp
Nov 15 '18 at 13:46
@yanick-salzmann I have added the declaration of StartUpdate. I dont do any magic with userdata in StartUpdate.
– Fabian Knapp
Nov 15 '18 at 13:46
@yanick-salzmann I have added the declaration of StartUpdate. I dont do any magic with userdata in StartUpdate.
– Fabian Knapp
Nov 15 '18 at 13:46
|
show 1 more 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%2f53320442%2fnested-stdbind-in-lambda-cannot-be-casted-to-stdfunction-from-void%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
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%2f53320442%2fnested-stdbind-in-lambda-cannot-be-casted-to-stdfunction-from-void%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
2
You know, that a lambda is not a pointer to an
std::function
and that your second snippet is probably only working because theoperator ()
ofstd::function
in the compiler you are using just happens to also work whenthis
is pointing to certain lambda types? Or is your code ofStartUpdate
doing something to get thatuserdata
to anstd::function
pointer?– Yanick Salzmann
Nov 15 '18 at 13:32
1
Please be more specific than ”it doesn’t work”. And read about the Minimal, Complete, and Verifiable example.
– molbdnilo
Nov 15 '18 at 13:35
@JeJo the
result
is whats passed through thestd::placeholders::_1
to the bound function.– Yanick Salzmann
Nov 15 '18 at 13:37
Thanks for your replys! @molbdnilo I know that my question is not that clear - thank you for the information. I will try to be more specific.
– Fabian Knapp
Nov 15 '18 at 13:46
@yanick-salzmann I have added the declaration of StartUpdate. I dont do any magic with userdata in StartUpdate.
– Fabian Knapp
Nov 15 '18 at 13:46