How to use goto statement in lambda expression C++












6














Is there any way to use goto statement in lambda expression?



#include <iostream>

int main()
{
auto lambda = () {
goto label;
return;
};
lambda();
return 0;
label:
std::cout << "hello, world!" << std::endl;
}


I want the console to output hello, world!, but the compiler gives an error:



use of undeclared label 'label'
goto label;
^
1 error generated.









share|improve this question


















  • 7




    This code is fairly confusing, you should really avoid using goto like that, the odds of nothing going wrong are fairly slim. If you could share what you actually wanted to achieve, we might be able to help with that.
    – Qubit
    Dec 17 '18 at 12:26








  • 1




    What's the problem you're trying to solve with goto inside a lambda?
    – Ami Tavory
    Dec 17 '18 at 12:28










  • @Qubit Maybe my example does not convey very well, but whether or not to use goto statement is not the key of this question.
    – 0x11901
    Dec 18 '18 at 3:18










  • @0x11901 My point was related more to the fact of even considering goto to move between functions, think of all the issues that arise from something like this, it would become fairly difficult to keep track of what the code does. Not to mention, you then have a function that never reaches the return statement, which seems to defeat the purpose. So the point was, there should never even be a case where you would want to do this, regardless of whether or not it is possible.
    – Qubit
    Dec 18 '18 at 8:39
















6














Is there any way to use goto statement in lambda expression?



#include <iostream>

int main()
{
auto lambda = () {
goto label;
return;
};
lambda();
return 0;
label:
std::cout << "hello, world!" << std::endl;
}


I want the console to output hello, world!, but the compiler gives an error:



use of undeclared label 'label'
goto label;
^
1 error generated.









share|improve this question


















  • 7




    This code is fairly confusing, you should really avoid using goto like that, the odds of nothing going wrong are fairly slim. If you could share what you actually wanted to achieve, we might be able to help with that.
    – Qubit
    Dec 17 '18 at 12:26








  • 1




    What's the problem you're trying to solve with goto inside a lambda?
    – Ami Tavory
    Dec 17 '18 at 12:28










  • @Qubit Maybe my example does not convey very well, but whether or not to use goto statement is not the key of this question.
    – 0x11901
    Dec 18 '18 at 3:18










  • @0x11901 My point was related more to the fact of even considering goto to move between functions, think of all the issues that arise from something like this, it would become fairly difficult to keep track of what the code does. Not to mention, you then have a function that never reaches the return statement, which seems to defeat the purpose. So the point was, there should never even be a case where you would want to do this, regardless of whether or not it is possible.
    – Qubit
    Dec 18 '18 at 8:39














6












6








6







Is there any way to use goto statement in lambda expression?



#include <iostream>

int main()
{
auto lambda = () {
goto label;
return;
};
lambda();
return 0;
label:
std::cout << "hello, world!" << std::endl;
}


I want the console to output hello, world!, but the compiler gives an error:



use of undeclared label 'label'
goto label;
^
1 error generated.









share|improve this question













Is there any way to use goto statement in lambda expression?



#include <iostream>

int main()
{
auto lambda = () {
goto label;
return;
};
lambda();
return 0;
label:
std::cout << "hello, world!" << std::endl;
}


I want the console to output hello, world!, but the compiler gives an error:



use of undeclared label 'label'
goto label;
^
1 error generated.






c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 17 '18 at 12:24









0x119010x11901

657




657








  • 7




    This code is fairly confusing, you should really avoid using goto like that, the odds of nothing going wrong are fairly slim. If you could share what you actually wanted to achieve, we might be able to help with that.
    – Qubit
    Dec 17 '18 at 12:26








  • 1




    What's the problem you're trying to solve with goto inside a lambda?
    – Ami Tavory
    Dec 17 '18 at 12:28










  • @Qubit Maybe my example does not convey very well, but whether or not to use goto statement is not the key of this question.
    – 0x11901
    Dec 18 '18 at 3:18










  • @0x11901 My point was related more to the fact of even considering goto to move between functions, think of all the issues that arise from something like this, it would become fairly difficult to keep track of what the code does. Not to mention, you then have a function that never reaches the return statement, which seems to defeat the purpose. So the point was, there should never even be a case where you would want to do this, regardless of whether or not it is possible.
    – Qubit
    Dec 18 '18 at 8:39














  • 7




    This code is fairly confusing, you should really avoid using goto like that, the odds of nothing going wrong are fairly slim. If you could share what you actually wanted to achieve, we might be able to help with that.
    – Qubit
    Dec 17 '18 at 12:26








  • 1




    What's the problem you're trying to solve with goto inside a lambda?
    – Ami Tavory
    Dec 17 '18 at 12:28










  • @Qubit Maybe my example does not convey very well, but whether or not to use goto statement is not the key of this question.
    – 0x11901
    Dec 18 '18 at 3:18










  • @0x11901 My point was related more to the fact of even considering goto to move between functions, think of all the issues that arise from something like this, it would become fairly difficult to keep track of what the code does. Not to mention, you then have a function that never reaches the return statement, which seems to defeat the purpose. So the point was, there should never even be a case where you would want to do this, regardless of whether or not it is possible.
    – Qubit
    Dec 18 '18 at 8:39








7




7




This code is fairly confusing, you should really avoid using goto like that, the odds of nothing going wrong are fairly slim. If you could share what you actually wanted to achieve, we might be able to help with that.
– Qubit
Dec 17 '18 at 12:26






This code is fairly confusing, you should really avoid using goto like that, the odds of nothing going wrong are fairly slim. If you could share what you actually wanted to achieve, we might be able to help with that.
– Qubit
Dec 17 '18 at 12:26






1




1




What's the problem you're trying to solve with goto inside a lambda?
– Ami Tavory
Dec 17 '18 at 12:28




What's the problem you're trying to solve with goto inside a lambda?
– Ami Tavory
Dec 17 '18 at 12:28












@Qubit Maybe my example does not convey very well, but whether or not to use goto statement is not the key of this question.
– 0x11901
Dec 18 '18 at 3:18




@Qubit Maybe my example does not convey very well, but whether or not to use goto statement is not the key of this question.
– 0x11901
Dec 18 '18 at 3:18












@0x11901 My point was related more to the fact of even considering goto to move between functions, think of all the issues that arise from something like this, it would become fairly difficult to keep track of what the code does. Not to mention, you then have a function that never reaches the return statement, which seems to defeat the purpose. So the point was, there should never even be a case where you would want to do this, regardless of whether or not it is possible.
– Qubit
Dec 18 '18 at 8:39




@0x11901 My point was related more to the fact of even considering goto to move between functions, think of all the issues that arise from something like this, it would become fairly difficult to keep track of what the code does. Not to mention, you then have a function that never reaches the return statement, which seems to defeat the purpose. So the point was, there should never even be a case where you would want to do this, regardless of whether or not it is possible.
– Qubit
Dec 18 '18 at 8:39












3 Answers
3






active

oldest

votes


















7














You can't use goto to move between functions, and a lambda defines a separate function to it's enclosing scope.



From this reference




The goto statement must be in the same function as the label it is referring, it may appear before or after the label.




And the standard, [stmt.goto]




The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label located in the current function.







share|improve this answer





























    14















    Is there any way to use goto statement in lambda expression?




    No. Not to leave the scope of lambda and jump to the enclosing scope. You can only goto a labeled statement inside the lambda, same as if it were any other function.



    Having said that, the uses for goto in C++ specifically are few and infrequent. There are other, better, options. I urge you not to reach for goto as the first tool you use.






    share|improve this answer

















    • 3




      Actually I can't come up with a single use case of goto in C++. In C, I still use it for error handling from time to time.
      – liliscent
      Dec 17 '18 at 12:40






    • 3




      @liliscent - I can't come up with one either (unless I'm in some sort of overly constraint environment). But I don't want to make an argument out of ignorance by saying there absolutely is no use.
      – StoryTeller
      Dec 17 '18 at 12:41








    • 1




      Some state machines are more clearly coded (and more efficient) using goto instead of other techniques. Some C-based SDKs (even when used with C++... say Carbon for example) use goto as the paradigm for error handling.
      – Eljay
      Dec 17 '18 at 12:47






    • 1




      @liliscent Isn't there one for breaking out of multiple loops? I can't think of a better one if you want to break out of all of them at once (even though most of the time it makes more sense to return, I can see a use there).
      – Spitemaster
      Dec 17 '18 at 18:53










    • @Spitemaster you use return to break out of a deep loop
      – Caleth
      Dec 17 '18 at 20:25



















    5














    The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring, it may appear before or after the label.



    You can instead do this:



    #include <iostream>

    int main()
    {
    auto lambda = () {
    goto label;
    return;
    label:
    std::cout << "hello, world!" << std::endl;
    };
    lambda();
    return 0;

    }


    And it will print "Hello World". See demo.






    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%2f53815203%2fhow-to-use-goto-statement-in-lambda-expression-c%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      You can't use goto to move between functions, and a lambda defines a separate function to it's enclosing scope.



      From this reference




      The goto statement must be in the same function as the label it is referring, it may appear before or after the label.




      And the standard, [stmt.goto]




      The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label located in the current function.







      share|improve this answer


























        7














        You can't use goto to move between functions, and a lambda defines a separate function to it's enclosing scope.



        From this reference




        The goto statement must be in the same function as the label it is referring, it may appear before or after the label.




        And the standard, [stmt.goto]




        The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label located in the current function.







        share|improve this answer
























          7












          7








          7






          You can't use goto to move between functions, and a lambda defines a separate function to it's enclosing scope.



          From this reference




          The goto statement must be in the same function as the label it is referring, it may appear before or after the label.




          And the standard, [stmt.goto]




          The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label located in the current function.







          share|improve this answer












          You can't use goto to move between functions, and a lambda defines a separate function to it's enclosing scope.



          From this reference




          The goto statement must be in the same function as the label it is referring, it may appear before or after the label.




          And the standard, [stmt.goto]




          The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label located in the current function.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 17 '18 at 12:28









          CalethCaleth

          17k22139




          17k22139

























              14















              Is there any way to use goto statement in lambda expression?




              No. Not to leave the scope of lambda and jump to the enclosing scope. You can only goto a labeled statement inside the lambda, same as if it were any other function.



              Having said that, the uses for goto in C++ specifically are few and infrequent. There are other, better, options. I urge you not to reach for goto as the first tool you use.






              share|improve this answer

















              • 3




                Actually I can't come up with a single use case of goto in C++. In C, I still use it for error handling from time to time.
                – liliscent
                Dec 17 '18 at 12:40






              • 3




                @liliscent - I can't come up with one either (unless I'm in some sort of overly constraint environment). But I don't want to make an argument out of ignorance by saying there absolutely is no use.
                – StoryTeller
                Dec 17 '18 at 12:41








              • 1




                Some state machines are more clearly coded (and more efficient) using goto instead of other techniques. Some C-based SDKs (even when used with C++... say Carbon for example) use goto as the paradigm for error handling.
                – Eljay
                Dec 17 '18 at 12:47






              • 1




                @liliscent Isn't there one for breaking out of multiple loops? I can't think of a better one if you want to break out of all of them at once (even though most of the time it makes more sense to return, I can see a use there).
                – Spitemaster
                Dec 17 '18 at 18:53










              • @Spitemaster you use return to break out of a deep loop
                – Caleth
                Dec 17 '18 at 20:25
















              14















              Is there any way to use goto statement in lambda expression?




              No. Not to leave the scope of lambda and jump to the enclosing scope. You can only goto a labeled statement inside the lambda, same as if it were any other function.



              Having said that, the uses for goto in C++ specifically are few and infrequent. There are other, better, options. I urge you not to reach for goto as the first tool you use.






              share|improve this answer

















              • 3




                Actually I can't come up with a single use case of goto in C++. In C, I still use it for error handling from time to time.
                – liliscent
                Dec 17 '18 at 12:40






              • 3




                @liliscent - I can't come up with one either (unless I'm in some sort of overly constraint environment). But I don't want to make an argument out of ignorance by saying there absolutely is no use.
                – StoryTeller
                Dec 17 '18 at 12:41








              • 1




                Some state machines are more clearly coded (and more efficient) using goto instead of other techniques. Some C-based SDKs (even when used with C++... say Carbon for example) use goto as the paradigm for error handling.
                – Eljay
                Dec 17 '18 at 12:47






              • 1




                @liliscent Isn't there one for breaking out of multiple loops? I can't think of a better one if you want to break out of all of them at once (even though most of the time it makes more sense to return, I can see a use there).
                – Spitemaster
                Dec 17 '18 at 18:53










              • @Spitemaster you use return to break out of a deep loop
                – Caleth
                Dec 17 '18 at 20:25














              14












              14








              14







              Is there any way to use goto statement in lambda expression?




              No. Not to leave the scope of lambda and jump to the enclosing scope. You can only goto a labeled statement inside the lambda, same as if it were any other function.



              Having said that, the uses for goto in C++ specifically are few and infrequent. There are other, better, options. I urge you not to reach for goto as the first tool you use.






              share|improve this answer













              Is there any way to use goto statement in lambda expression?




              No. Not to leave the scope of lambda and jump to the enclosing scope. You can only goto a labeled statement inside the lambda, same as if it were any other function.



              Having said that, the uses for goto in C++ specifically are few and infrequent. There are other, better, options. I urge you not to reach for goto as the first tool you use.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 17 '18 at 12:28









              StoryTellerStoryTeller

              94.5k12191254




              94.5k12191254








              • 3




                Actually I can't come up with a single use case of goto in C++. In C, I still use it for error handling from time to time.
                – liliscent
                Dec 17 '18 at 12:40






              • 3




                @liliscent - I can't come up with one either (unless I'm in some sort of overly constraint environment). But I don't want to make an argument out of ignorance by saying there absolutely is no use.
                – StoryTeller
                Dec 17 '18 at 12:41








              • 1




                Some state machines are more clearly coded (and more efficient) using goto instead of other techniques. Some C-based SDKs (even when used with C++... say Carbon for example) use goto as the paradigm for error handling.
                – Eljay
                Dec 17 '18 at 12:47






              • 1




                @liliscent Isn't there one for breaking out of multiple loops? I can't think of a better one if you want to break out of all of them at once (even though most of the time it makes more sense to return, I can see a use there).
                – Spitemaster
                Dec 17 '18 at 18:53










              • @Spitemaster you use return to break out of a deep loop
                – Caleth
                Dec 17 '18 at 20:25














              • 3




                Actually I can't come up with a single use case of goto in C++. In C, I still use it for error handling from time to time.
                – liliscent
                Dec 17 '18 at 12:40






              • 3




                @liliscent - I can't come up with one either (unless I'm in some sort of overly constraint environment). But I don't want to make an argument out of ignorance by saying there absolutely is no use.
                – StoryTeller
                Dec 17 '18 at 12:41








              • 1




                Some state machines are more clearly coded (and more efficient) using goto instead of other techniques. Some C-based SDKs (even when used with C++... say Carbon for example) use goto as the paradigm for error handling.
                – Eljay
                Dec 17 '18 at 12:47






              • 1




                @liliscent Isn't there one for breaking out of multiple loops? I can't think of a better one if you want to break out of all of them at once (even though most of the time it makes more sense to return, I can see a use there).
                – Spitemaster
                Dec 17 '18 at 18:53










              • @Spitemaster you use return to break out of a deep loop
                – Caleth
                Dec 17 '18 at 20:25








              3




              3




              Actually I can't come up with a single use case of goto in C++. In C, I still use it for error handling from time to time.
              – liliscent
              Dec 17 '18 at 12:40




              Actually I can't come up with a single use case of goto in C++. In C, I still use it for error handling from time to time.
              – liliscent
              Dec 17 '18 at 12:40




              3




              3




              @liliscent - I can't come up with one either (unless I'm in some sort of overly constraint environment). But I don't want to make an argument out of ignorance by saying there absolutely is no use.
              – StoryTeller
              Dec 17 '18 at 12:41






              @liliscent - I can't come up with one either (unless I'm in some sort of overly constraint environment). But I don't want to make an argument out of ignorance by saying there absolutely is no use.
              – StoryTeller
              Dec 17 '18 at 12:41






              1




              1




              Some state machines are more clearly coded (and more efficient) using goto instead of other techniques. Some C-based SDKs (even when used with C++... say Carbon for example) use goto as the paradigm for error handling.
              – Eljay
              Dec 17 '18 at 12:47




              Some state machines are more clearly coded (and more efficient) using goto instead of other techniques. Some C-based SDKs (even when used with C++... say Carbon for example) use goto as the paradigm for error handling.
              – Eljay
              Dec 17 '18 at 12:47




              1




              1




              @liliscent Isn't there one for breaking out of multiple loops? I can't think of a better one if you want to break out of all of them at once (even though most of the time it makes more sense to return, I can see a use there).
              – Spitemaster
              Dec 17 '18 at 18:53




              @liliscent Isn't there one for breaking out of multiple loops? I can't think of a better one if you want to break out of all of them at once (even though most of the time it makes more sense to return, I can see a use there).
              – Spitemaster
              Dec 17 '18 at 18:53












              @Spitemaster you use return to break out of a deep loop
              – Caleth
              Dec 17 '18 at 20:25




              @Spitemaster you use return to break out of a deep loop
              – Caleth
              Dec 17 '18 at 20:25











              5














              The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring, it may appear before or after the label.



              You can instead do this:



              #include <iostream>

              int main()
              {
              auto lambda = () {
              goto label;
              return;
              label:
              std::cout << "hello, world!" << std::endl;
              };
              lambda();
              return 0;

              }


              And it will print "Hello World". See demo.






              share|improve this answer


























                5














                The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring, it may appear before or after the label.



                You can instead do this:



                #include <iostream>

                int main()
                {
                auto lambda = () {
                goto label;
                return;
                label:
                std::cout << "hello, world!" << std::endl;
                };
                lambda();
                return 0;

                }


                And it will print "Hello World". See demo.






                share|improve this answer
























                  5












                  5








                  5






                  The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring, it may appear before or after the label.



                  You can instead do this:



                  #include <iostream>

                  int main()
                  {
                  auto lambda = () {
                  goto label;
                  return;
                  label:
                  std::cout << "hello, world!" << std::endl;
                  };
                  lambda();
                  return 0;

                  }


                  And it will print "Hello World". See demo.






                  share|improve this answer












                  The goto statement transfers control to the location specified by label. The goto statement must be in the same function as the label it is referring, it may appear before or after the label.



                  You can instead do this:



                  #include <iostream>

                  int main()
                  {
                  auto lambda = () {
                  goto label;
                  return;
                  label:
                  std::cout << "hello, world!" << std::endl;
                  };
                  lambda();
                  return 0;

                  }


                  And it will print "Hello World". See demo.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 17 '18 at 12:28









                  P.WP.W

                  11.8k3842




                  11.8k3842






























                      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%2f53815203%2fhow-to-use-goto-statement-in-lambda-expression-c%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







                      Popular posts from this blog

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]