error: invalid initialization of non-const reference of type 'std::function&' from an rvalue of type 'main()::'|










-1















EDIT: Sorry, I asked this question without a thro understanding of references...



I seem to be getting this error when I run this code...



error: invalid initialization of non-const reference of type 'std::function&' from an rvalue of type 'main()::'



#include <bits/stdc++.h>
using namespace std ;

void printfunction(bool a, function <void()> &b)

if (a == true)

b() ;



int main()

int value = 45 ;

printfunction(true, [value]()cout << "The value is : " << value ;) ;



But, the error disappears when I add a const before function... like this :



void printfunction(bool a,const function <void()> &b) 


The thing is I would like to change the function in the function reference if needed...
Is there any other way to do this? Please let me know if it does indeed exist.



P.S: I am using Code:: Blocks 16.01



Bye,



Samuel










share|improve this question
























  • C syntax error, C tag removed

    – pmg
    Nov 15 '18 at 14:18











  • Why aren't you taking the function by value then?

    – Quentin
    Nov 15 '18 at 14:20











  • Something doesn't make sense here. If you change the function, the changes will be lost anyway since the caller creates a temporary function. So if you want to change the function in printfunction, you're calling it wrong. Do you want printfunction to get a private copy of the function that it can then modify without affecting its caller?

    – David Schwartz
    Nov 15 '18 at 14:37
















-1















EDIT: Sorry, I asked this question without a thro understanding of references...



I seem to be getting this error when I run this code...



error: invalid initialization of non-const reference of type 'std::function&' from an rvalue of type 'main()::'



#include <bits/stdc++.h>
using namespace std ;

void printfunction(bool a, function <void()> &b)

if (a == true)

b() ;



int main()

int value = 45 ;

printfunction(true, [value]()cout << "The value is : " << value ;) ;



But, the error disappears when I add a const before function... like this :



void printfunction(bool a,const function <void()> &b) 


The thing is I would like to change the function in the function reference if needed...
Is there any other way to do this? Please let me know if it does indeed exist.



P.S: I am using Code:: Blocks 16.01



Bye,



Samuel










share|improve this question
























  • C syntax error, C tag removed

    – pmg
    Nov 15 '18 at 14:18











  • Why aren't you taking the function by value then?

    – Quentin
    Nov 15 '18 at 14:20











  • Something doesn't make sense here. If you change the function, the changes will be lost anyway since the caller creates a temporary function. So if you want to change the function in printfunction, you're calling it wrong. Do you want printfunction to get a private copy of the function that it can then modify without affecting its caller?

    – David Schwartz
    Nov 15 '18 at 14:37














-1












-1








-1


0






EDIT: Sorry, I asked this question without a thro understanding of references...



I seem to be getting this error when I run this code...



error: invalid initialization of non-const reference of type 'std::function&' from an rvalue of type 'main()::'



#include <bits/stdc++.h>
using namespace std ;

void printfunction(bool a, function <void()> &b)

if (a == true)

b() ;



int main()

int value = 45 ;

printfunction(true, [value]()cout << "The value is : " << value ;) ;



But, the error disappears when I add a const before function... like this :



void printfunction(bool a,const function <void()> &b) 


The thing is I would like to change the function in the function reference if needed...
Is there any other way to do this? Please let me know if it does indeed exist.



P.S: I am using Code:: Blocks 16.01



Bye,



Samuel










share|improve this question
















EDIT: Sorry, I asked this question without a thro understanding of references...



I seem to be getting this error when I run this code...



error: invalid initialization of non-const reference of type 'std::function&' from an rvalue of type 'main()::'



#include <bits/stdc++.h>
using namespace std ;

void printfunction(bool a, function <void()> &b)

if (a == true)

b() ;



int main()

int value = 45 ;

printfunction(true, [value]()cout << "The value is : " << value ;) ;



But, the error disappears when I add a const before function... like this :



void printfunction(bool a,const function <void()> &b) 


The thing is I would like to change the function in the function reference if needed...
Is there any other way to do this? Please let me know if it does indeed exist.



P.S: I am using Code:: Blocks 16.01



Bye,



Samuel







c++ rvalue lvalue lvalue-to-rvalue






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 '18 at 14:24







Samuel Rasquinha

















asked Nov 15 '18 at 14:17









Samuel RasquinhaSamuel Rasquinha

1918




1918












  • C syntax error, C tag removed

    – pmg
    Nov 15 '18 at 14:18











  • Why aren't you taking the function by value then?

    – Quentin
    Nov 15 '18 at 14:20











  • Something doesn't make sense here. If you change the function, the changes will be lost anyway since the caller creates a temporary function. So if you want to change the function in printfunction, you're calling it wrong. Do you want printfunction to get a private copy of the function that it can then modify without affecting its caller?

    – David Schwartz
    Nov 15 '18 at 14:37


















  • C syntax error, C tag removed

    – pmg
    Nov 15 '18 at 14:18











  • Why aren't you taking the function by value then?

    – Quentin
    Nov 15 '18 at 14:20











  • Something doesn't make sense here. If you change the function, the changes will be lost anyway since the caller creates a temporary function. So if you want to change the function in printfunction, you're calling it wrong. Do you want printfunction to get a private copy of the function that it can then modify without affecting its caller?

    – David Schwartz
    Nov 15 '18 at 14:37

















C syntax error, C tag removed

– pmg
Nov 15 '18 at 14:18





C syntax error, C tag removed

– pmg
Nov 15 '18 at 14:18













Why aren't you taking the function by value then?

– Quentin
Nov 15 '18 at 14:20





Why aren't you taking the function by value then?

– Quentin
Nov 15 '18 at 14:20













Something doesn't make sense here. If you change the function, the changes will be lost anyway since the caller creates a temporary function. So if you want to change the function in printfunction, you're calling it wrong. Do you want printfunction to get a private copy of the function that it can then modify without affecting its caller?

– David Schwartz
Nov 15 '18 at 14:37






Something doesn't make sense here. If you change the function, the changes will be lost anyway since the caller creates a temporary function. So if you want to change the function in printfunction, you're calling it wrong. Do you want printfunction to get a private copy of the function that it can then modify without affecting its caller?

– David Schwartz
Nov 15 '18 at 14:37













2 Answers
2






active

oldest

votes


















2














In printfunction call, lambda expression [value]() ... argument must be converted to a temporary function<void()> object first.



A reference to non-const function<void()>& only binds to l-values, not temporaries (r-values).



A reference to const, on the other hand, can be bound to temporaries. Which is what you observe.






share|improve this answer
































    2














    If you want to modify the std::function, then you'll need to pass a modifiable (lvalue) parameter:



    int main()

    int value = 45;

    std::function f = [value]() std::cout << "The value is : " << value ;;

    printfunction(true, f);



    What you were trying to do isn't much different from writing a function that takes a mutable reference to int (e.g. void foo(int& x)) and then complaining that you can't call foo(5). (The small difference is that the the lambda-expression is converted to a temporary std::function - but that still can't be bound to a non-const reference).




    An alternative would be to change printfunction to take its argument by value rather than by reference, so that it has its own copy which it may modify. You'll have to consider the needs of the caller to decide whether that's more appropriate.






    share|improve this answer
























      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%2f53321443%2ferror-invalid-initialization-of-non-const-reference-of-type-stdfunctionvoid%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      In printfunction call, lambda expression [value]() ... argument must be converted to a temporary function<void()> object first.



      A reference to non-const function<void()>& only binds to l-values, not temporaries (r-values).



      A reference to const, on the other hand, can be bound to temporaries. Which is what you observe.






      share|improve this answer





























        2














        In printfunction call, lambda expression [value]() ... argument must be converted to a temporary function<void()> object first.



        A reference to non-const function<void()>& only binds to l-values, not temporaries (r-values).



        A reference to const, on the other hand, can be bound to temporaries. Which is what you observe.






        share|improve this answer



























          2












          2








          2







          In printfunction call, lambda expression [value]() ... argument must be converted to a temporary function<void()> object first.



          A reference to non-const function<void()>& only binds to l-values, not temporaries (r-values).



          A reference to const, on the other hand, can be bound to temporaries. Which is what you observe.






          share|improve this answer















          In printfunction call, lambda expression [value]() ... argument must be converted to a temporary function<void()> object first.



          A reference to non-const function<void()>& only binds to l-values, not temporaries (r-values).



          A reference to const, on the other hand, can be bound to temporaries. Which is what you observe.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 14:33

























          answered Nov 15 '18 at 14:27









          Maxim EgorushkinMaxim Egorushkin

          89k11103191




          89k11103191























              2














              If you want to modify the std::function, then you'll need to pass a modifiable (lvalue) parameter:



              int main()

              int value = 45;

              std::function f = [value]() std::cout << "The value is : " << value ;;

              printfunction(true, f);



              What you were trying to do isn't much different from writing a function that takes a mutable reference to int (e.g. void foo(int& x)) and then complaining that you can't call foo(5). (The small difference is that the the lambda-expression is converted to a temporary std::function - but that still can't be bound to a non-const reference).




              An alternative would be to change printfunction to take its argument by value rather than by reference, so that it has its own copy which it may modify. You'll have to consider the needs of the caller to decide whether that's more appropriate.






              share|improve this answer





























                2














                If you want to modify the std::function, then you'll need to pass a modifiable (lvalue) parameter:



                int main()

                int value = 45;

                std::function f = [value]() std::cout << "The value is : " << value ;;

                printfunction(true, f);



                What you were trying to do isn't much different from writing a function that takes a mutable reference to int (e.g. void foo(int& x)) and then complaining that you can't call foo(5). (The small difference is that the the lambda-expression is converted to a temporary std::function - but that still can't be bound to a non-const reference).




                An alternative would be to change printfunction to take its argument by value rather than by reference, so that it has its own copy which it may modify. You'll have to consider the needs of the caller to decide whether that's more appropriate.






                share|improve this answer



























                  2












                  2








                  2







                  If you want to modify the std::function, then you'll need to pass a modifiable (lvalue) parameter:



                  int main()

                  int value = 45;

                  std::function f = [value]() std::cout << "The value is : " << value ;;

                  printfunction(true, f);



                  What you were trying to do isn't much different from writing a function that takes a mutable reference to int (e.g. void foo(int& x)) and then complaining that you can't call foo(5). (The small difference is that the the lambda-expression is converted to a temporary std::function - but that still can't be bound to a non-const reference).




                  An alternative would be to change printfunction to take its argument by value rather than by reference, so that it has its own copy which it may modify. You'll have to consider the needs of the caller to decide whether that's more appropriate.






                  share|improve this answer















                  If you want to modify the std::function, then you'll need to pass a modifiable (lvalue) parameter:



                  int main()

                  int value = 45;

                  std::function f = [value]() std::cout << "The value is : " << value ;;

                  printfunction(true, f);



                  What you were trying to do isn't much different from writing a function that takes a mutable reference to int (e.g. void foo(int& x)) and then complaining that you can't call foo(5). (The small difference is that the the lambda-expression is converted to a temporary std::function - but that still can't be bound to a non-const reference).




                  An alternative would be to change printfunction to take its argument by value rather than by reference, so that it has its own copy which it may modify. You'll have to consider the needs of the caller to decide whether that's more appropriate.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 14:34

























                  answered Nov 15 '18 at 14:28









                  Toby SpeightToby Speight

                  17.1k134367




                  17.1k134367



























                      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%2f53321443%2ferror-invalid-initialization-of-non-const-reference-of-type-stdfunctionvoid%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto