C++11 constexpr function pass parameter












9















Consider the following code:



static constexpr int make_const(const int i){
return i;
}

void t1(const int i)
{
constexpr int ii = make_const(i); // error occurs here (i is not a constant expression)
std::cout<<ii;
}

int main()
{
t1(12);
}


Why I have an error on make_const call?





UPDATE



But this one works:



constexpr int t1(const int i)
{
return make_const(i);
}


However, this not:



template<int i>
constexpr bool do_something(){
return i;
}

constexpr int t1(const int i)
{
return do_something<make_const(i)>(); // error occurs here (i is not a constant expression)
}









share|improve this question




















  • 1





    Well because in the general case, i isn't constexpr in void t1(const int).

    – stefan
    May 9 '14 at 16:15






  • 1





    how can I make it constexpr, then?

    – tower120
    May 9 '14 at 16:21











  • making it a template argument is your only option

    – stefan
    May 9 '14 at 16:22











  • Your update does compile (though it does something completely different), what do you mean it doesn't work? (the const is useless)

    – Marc Glisse
    May 9 '14 at 16:34






  • 3





    There is no direct way to do what you want to do. This feature/limitation is probably the most frequently asked question about constexpr.

    – Marc Glisse
    May 9 '14 at 21:06
















9















Consider the following code:



static constexpr int make_const(const int i){
return i;
}

void t1(const int i)
{
constexpr int ii = make_const(i); // error occurs here (i is not a constant expression)
std::cout<<ii;
}

int main()
{
t1(12);
}


Why I have an error on make_const call?





UPDATE



But this one works:



constexpr int t1(const int i)
{
return make_const(i);
}


However, this not:



template<int i>
constexpr bool do_something(){
return i;
}

constexpr int t1(const int i)
{
return do_something<make_const(i)>(); // error occurs here (i is not a constant expression)
}









share|improve this question




















  • 1





    Well because in the general case, i isn't constexpr in void t1(const int).

    – stefan
    May 9 '14 at 16:15






  • 1





    how can I make it constexpr, then?

    – tower120
    May 9 '14 at 16:21











  • making it a template argument is your only option

    – stefan
    May 9 '14 at 16:22











  • Your update does compile (though it does something completely different), what do you mean it doesn't work? (the const is useless)

    – Marc Glisse
    May 9 '14 at 16:34






  • 3





    There is no direct way to do what you want to do. This feature/limitation is probably the most frequently asked question about constexpr.

    – Marc Glisse
    May 9 '14 at 21:06














9












9








9


4






Consider the following code:



static constexpr int make_const(const int i){
return i;
}

void t1(const int i)
{
constexpr int ii = make_const(i); // error occurs here (i is not a constant expression)
std::cout<<ii;
}

int main()
{
t1(12);
}


Why I have an error on make_const call?





UPDATE



But this one works:



constexpr int t1(const int i)
{
return make_const(i);
}


However, this not:



template<int i>
constexpr bool do_something(){
return i;
}

constexpr int t1(const int i)
{
return do_something<make_const(i)>(); // error occurs here (i is not a constant expression)
}









share|improve this question
















Consider the following code:



static constexpr int make_const(const int i){
return i;
}

void t1(const int i)
{
constexpr int ii = make_const(i); // error occurs here (i is not a constant expression)
std::cout<<ii;
}

int main()
{
t1(12);
}


Why I have an error on make_const call?





UPDATE



But this one works:



constexpr int t1(const int i)
{
return make_const(i);
}


However, this not:



template<int i>
constexpr bool do_something(){
return i;
}

constexpr int t1(const int i)
{
return do_something<make_const(i)>(); // error occurs here (i is not a constant expression)
}






c++ c++11 constexpr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 9 '14 at 17:09







tower120

















asked May 9 '14 at 16:09









tower120tower120

1,84712251




1,84712251








  • 1





    Well because in the general case, i isn't constexpr in void t1(const int).

    – stefan
    May 9 '14 at 16:15






  • 1





    how can I make it constexpr, then?

    – tower120
    May 9 '14 at 16:21











  • making it a template argument is your only option

    – stefan
    May 9 '14 at 16:22











  • Your update does compile (though it does something completely different), what do you mean it doesn't work? (the const is useless)

    – Marc Glisse
    May 9 '14 at 16:34






  • 3





    There is no direct way to do what you want to do. This feature/limitation is probably the most frequently asked question about constexpr.

    – Marc Glisse
    May 9 '14 at 21:06














  • 1





    Well because in the general case, i isn't constexpr in void t1(const int).

    – stefan
    May 9 '14 at 16:15






  • 1





    how can I make it constexpr, then?

    – tower120
    May 9 '14 at 16:21











  • making it a template argument is your only option

    – stefan
    May 9 '14 at 16:22











  • Your update does compile (though it does something completely different), what do you mean it doesn't work? (the const is useless)

    – Marc Glisse
    May 9 '14 at 16:34






  • 3





    There is no direct way to do what you want to do. This feature/limitation is probably the most frequently asked question about constexpr.

    – Marc Glisse
    May 9 '14 at 21:06








1




1





Well because in the general case, i isn't constexpr in void t1(const int).

– stefan
May 9 '14 at 16:15





Well because in the general case, i isn't constexpr in void t1(const int).

– stefan
May 9 '14 at 16:15




1




1





how can I make it constexpr, then?

– tower120
May 9 '14 at 16:21





how can I make it constexpr, then?

– tower120
May 9 '14 at 16:21













making it a template argument is your only option

– stefan
May 9 '14 at 16:22





making it a template argument is your only option

– stefan
May 9 '14 at 16:22













Your update does compile (though it does something completely different), what do you mean it doesn't work? (the const is useless)

– Marc Glisse
May 9 '14 at 16:34





Your update does compile (though it does something completely different), what do you mean it doesn't work? (the const is useless)

– Marc Glisse
May 9 '14 at 16:34




3




3





There is no direct way to do what you want to do. This feature/limitation is probably the most frequently asked question about constexpr.

– Marc Glisse
May 9 '14 at 21:06





There is no direct way to do what you want to do. This feature/limitation is probably the most frequently asked question about constexpr.

– Marc Glisse
May 9 '14 at 21:06












3 Answers
3






active

oldest

votes


















12














A constexpr function and a constexpr variable are related, but different things.



A constexpr variable is a variable whose value is guaranteed to be available at compile time.



A constexpr function is a function that, if evaluated with constexpr arguments, and behaves "properly" during its execution, will be evaluated at compile time.



If you pass a non-constexpr int to a constexpr function, it will not magically make it evaluated at compile time. It will, however, be allowed to pass the constexprness of its input parameters through itself (normal functions cannot do this).



constexpr on functions is a mixture of documentation and restriction on how they are written and instructions to the compiler.



The reason behind this is to allow the same function to be evaluated both at compile time, and at run time. If passed runtime arguments, it is a runtime function. If passed constexpr arguments, it may be evaluated at compile time (and will be if used in certain contexts).






share|improve this answer































    6














    One important difference between const and constexpr is that a constexpr can be evaluated at compile time.



    By writing constexpr int ii = make_const(i); you are telling the compiler that the expression is to be evaluted at compile time. Since i is evaluted at run-time, the compiler is unable to do this and gives you an error.






    share|improve this answer































      2














      Because t1() is not a constexpr function, the parameter i is a runtime variable... which you can't pass to a constexpr function. Constexpr expects the parameter to be known at compile time.






      share|improve this answer
























      • Making t1 constexpr wouldn't change anything.

        – Marc Glisse
        May 9 '14 at 16:19











      • Yes, I test your suggestion. Same old story.

        – tower120
        May 9 '14 at 16:20











      • @MarcGlisse t1() is not correctly formed to be a constexpr function, I wasn't suggesting making it so would correct the problem.

        – paxos1977
        May 9 '14 at 16:20











      • @praxos1977 - see updated question. Your suggestion is wrong.

        – tower120
        May 9 '14 at 16:23






      • 2





        @tower120, I wasn't making a suggestion. I was pointing out that you can't pass a runtime variable to a constexpr function. TAS obviously worded his answer better than I did.

        – paxos1977
        May 9 '14 at 16:24














      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%2f23569309%2fc11-constexpr-function-pass-parameter%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









      12














      A constexpr function and a constexpr variable are related, but different things.



      A constexpr variable is a variable whose value is guaranteed to be available at compile time.



      A constexpr function is a function that, if evaluated with constexpr arguments, and behaves "properly" during its execution, will be evaluated at compile time.



      If you pass a non-constexpr int to a constexpr function, it will not magically make it evaluated at compile time. It will, however, be allowed to pass the constexprness of its input parameters through itself (normal functions cannot do this).



      constexpr on functions is a mixture of documentation and restriction on how they are written and instructions to the compiler.



      The reason behind this is to allow the same function to be evaluated both at compile time, and at run time. If passed runtime arguments, it is a runtime function. If passed constexpr arguments, it may be evaluated at compile time (and will be if used in certain contexts).






      share|improve this answer




























        12














        A constexpr function and a constexpr variable are related, but different things.



        A constexpr variable is a variable whose value is guaranteed to be available at compile time.



        A constexpr function is a function that, if evaluated with constexpr arguments, and behaves "properly" during its execution, will be evaluated at compile time.



        If you pass a non-constexpr int to a constexpr function, it will not magically make it evaluated at compile time. It will, however, be allowed to pass the constexprness of its input parameters through itself (normal functions cannot do this).



        constexpr on functions is a mixture of documentation and restriction on how they are written and instructions to the compiler.



        The reason behind this is to allow the same function to be evaluated both at compile time, and at run time. If passed runtime arguments, it is a runtime function. If passed constexpr arguments, it may be evaluated at compile time (and will be if used in certain contexts).






        share|improve this answer


























          12












          12








          12







          A constexpr function and a constexpr variable are related, but different things.



          A constexpr variable is a variable whose value is guaranteed to be available at compile time.



          A constexpr function is a function that, if evaluated with constexpr arguments, and behaves "properly" during its execution, will be evaluated at compile time.



          If you pass a non-constexpr int to a constexpr function, it will not magically make it evaluated at compile time. It will, however, be allowed to pass the constexprness of its input parameters through itself (normal functions cannot do this).



          constexpr on functions is a mixture of documentation and restriction on how they are written and instructions to the compiler.



          The reason behind this is to allow the same function to be evaluated both at compile time, and at run time. If passed runtime arguments, it is a runtime function. If passed constexpr arguments, it may be evaluated at compile time (and will be if used in certain contexts).






          share|improve this answer













          A constexpr function and a constexpr variable are related, but different things.



          A constexpr variable is a variable whose value is guaranteed to be available at compile time.



          A constexpr function is a function that, if evaluated with constexpr arguments, and behaves "properly" during its execution, will be evaluated at compile time.



          If you pass a non-constexpr int to a constexpr function, it will not magically make it evaluated at compile time. It will, however, be allowed to pass the constexprness of its input parameters through itself (normal functions cannot do this).



          constexpr on functions is a mixture of documentation and restriction on how they are written and instructions to the compiler.



          The reason behind this is to allow the same function to be evaluated both at compile time, and at run time. If passed runtime arguments, it is a runtime function. If passed constexpr arguments, it may be evaluated at compile time (and will be if used in certain contexts).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 9 '14 at 17:52









          Yakk - Adam NevraumontYakk - Adam Nevraumont

          188k21199384




          188k21199384

























              6














              One important difference between const and constexpr is that a constexpr can be evaluated at compile time.



              By writing constexpr int ii = make_const(i); you are telling the compiler that the expression is to be evaluted at compile time. Since i is evaluted at run-time, the compiler is unable to do this and gives you an error.






              share|improve this answer




























                6














                One important difference between const and constexpr is that a constexpr can be evaluated at compile time.



                By writing constexpr int ii = make_const(i); you are telling the compiler that the expression is to be evaluted at compile time. Since i is evaluted at run-time, the compiler is unable to do this and gives you an error.






                share|improve this answer


























                  6












                  6








                  6







                  One important difference between const and constexpr is that a constexpr can be evaluated at compile time.



                  By writing constexpr int ii = make_const(i); you are telling the compiler that the expression is to be evaluted at compile time. Since i is evaluted at run-time, the compiler is unable to do this and gives you an error.






                  share|improve this answer













                  One important difference between const and constexpr is that a constexpr can be evaluated at compile time.



                  By writing constexpr int ii = make_const(i); you are telling the compiler that the expression is to be evaluted at compile time. Since i is evaluted at run-time, the compiler is unable to do this and gives you an error.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 9 '14 at 16:23









                  TASTAS

                  1,8961015




                  1,8961015























                      2














                      Because t1() is not a constexpr function, the parameter i is a runtime variable... which you can't pass to a constexpr function. Constexpr expects the parameter to be known at compile time.






                      share|improve this answer
























                      • Making t1 constexpr wouldn't change anything.

                        – Marc Glisse
                        May 9 '14 at 16:19











                      • Yes, I test your suggestion. Same old story.

                        – tower120
                        May 9 '14 at 16:20











                      • @MarcGlisse t1() is not correctly formed to be a constexpr function, I wasn't suggesting making it so would correct the problem.

                        – paxos1977
                        May 9 '14 at 16:20











                      • @praxos1977 - see updated question. Your suggestion is wrong.

                        – tower120
                        May 9 '14 at 16:23






                      • 2





                        @tower120, I wasn't making a suggestion. I was pointing out that you can't pass a runtime variable to a constexpr function. TAS obviously worded his answer better than I did.

                        – paxos1977
                        May 9 '14 at 16:24


















                      2














                      Because t1() is not a constexpr function, the parameter i is a runtime variable... which you can't pass to a constexpr function. Constexpr expects the parameter to be known at compile time.






                      share|improve this answer
























                      • Making t1 constexpr wouldn't change anything.

                        – Marc Glisse
                        May 9 '14 at 16:19











                      • Yes, I test your suggestion. Same old story.

                        – tower120
                        May 9 '14 at 16:20











                      • @MarcGlisse t1() is not correctly formed to be a constexpr function, I wasn't suggesting making it so would correct the problem.

                        – paxos1977
                        May 9 '14 at 16:20











                      • @praxos1977 - see updated question. Your suggestion is wrong.

                        – tower120
                        May 9 '14 at 16:23






                      • 2





                        @tower120, I wasn't making a suggestion. I was pointing out that you can't pass a runtime variable to a constexpr function. TAS obviously worded his answer better than I did.

                        – paxos1977
                        May 9 '14 at 16:24
















                      2












                      2








                      2







                      Because t1() is not a constexpr function, the parameter i is a runtime variable... which you can't pass to a constexpr function. Constexpr expects the parameter to be known at compile time.






                      share|improve this answer













                      Because t1() is not a constexpr function, the parameter i is a runtime variable... which you can't pass to a constexpr function. Constexpr expects the parameter to be known at compile time.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered May 9 '14 at 16:16









                      paxos1977paxos1977

                      62.1k2278116




                      62.1k2278116













                      • Making t1 constexpr wouldn't change anything.

                        – Marc Glisse
                        May 9 '14 at 16:19











                      • Yes, I test your suggestion. Same old story.

                        – tower120
                        May 9 '14 at 16:20











                      • @MarcGlisse t1() is not correctly formed to be a constexpr function, I wasn't suggesting making it so would correct the problem.

                        – paxos1977
                        May 9 '14 at 16:20











                      • @praxos1977 - see updated question. Your suggestion is wrong.

                        – tower120
                        May 9 '14 at 16:23






                      • 2





                        @tower120, I wasn't making a suggestion. I was pointing out that you can't pass a runtime variable to a constexpr function. TAS obviously worded his answer better than I did.

                        – paxos1977
                        May 9 '14 at 16:24





















                      • Making t1 constexpr wouldn't change anything.

                        – Marc Glisse
                        May 9 '14 at 16:19











                      • Yes, I test your suggestion. Same old story.

                        – tower120
                        May 9 '14 at 16:20











                      • @MarcGlisse t1() is not correctly formed to be a constexpr function, I wasn't suggesting making it so would correct the problem.

                        – paxos1977
                        May 9 '14 at 16:20











                      • @praxos1977 - see updated question. Your suggestion is wrong.

                        – tower120
                        May 9 '14 at 16:23






                      • 2





                        @tower120, I wasn't making a suggestion. I was pointing out that you can't pass a runtime variable to a constexpr function. TAS obviously worded his answer better than I did.

                        – paxos1977
                        May 9 '14 at 16:24



















                      Making t1 constexpr wouldn't change anything.

                      – Marc Glisse
                      May 9 '14 at 16:19





                      Making t1 constexpr wouldn't change anything.

                      – Marc Glisse
                      May 9 '14 at 16:19













                      Yes, I test your suggestion. Same old story.

                      – tower120
                      May 9 '14 at 16:20





                      Yes, I test your suggestion. Same old story.

                      – tower120
                      May 9 '14 at 16:20













                      @MarcGlisse t1() is not correctly formed to be a constexpr function, I wasn't suggesting making it so would correct the problem.

                      – paxos1977
                      May 9 '14 at 16:20





                      @MarcGlisse t1() is not correctly formed to be a constexpr function, I wasn't suggesting making it so would correct the problem.

                      – paxos1977
                      May 9 '14 at 16:20













                      @praxos1977 - see updated question. Your suggestion is wrong.

                      – tower120
                      May 9 '14 at 16:23





                      @praxos1977 - see updated question. Your suggestion is wrong.

                      – tower120
                      May 9 '14 at 16:23




                      2




                      2





                      @tower120, I wasn't making a suggestion. I was pointing out that you can't pass a runtime variable to a constexpr function. TAS obviously worded his answer better than I did.

                      – paxos1977
                      May 9 '14 at 16:24







                      @tower120, I wasn't making a suggestion. I was pointing out that you can't pass a runtime variable to a constexpr function. TAS obviously worded his answer better than I did.

                      – paxos1977
                      May 9 '14 at 16:24




















                      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%2f23569309%2fc11-constexpr-function-pass-parameter%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

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith