constexpr function contains a const - will it ever be evaluated at compile time?












1















Please can someone clarify (I'm using Visual Studio 15.9.2):



In the following code given that Pi_cnst is evaluated at run time (because defining Pi in this way requires a run time calculation), will RAD2DEG_cnst ever be evaluated at compile time or is the use of "constexpr" always reverting to "const" ?



edit - add: If it is always reverting to const then should I expect a warning, or is it bad in some other way i.e. it seems odd to be able to so easily declare a constexpr for the body to be accepted but ALWAYS be such that it is never actually a constexpr. What have I missed?



constexpr double Pi_error = std::acos(-1.0); //error function call must have a constant value in a constant expression (does not compile)
const double Pi_cnst = std::acos(-1.0); //ok evaluated at run time

constexpr double Pi_expr = 3.1415926; //ok valid constexpr

constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; } //no error or warning BUT is this ever evaluated at compile time?
constexpr double RAD2DEG_expr(double rad) { return rad * 180.0 / Pi_expr; } //ok can be evaluated at compile time

const double result1 = RAD2DEG_cnst(0.1); //evaluated at run time?
const double result2 = RAD2DEG_expr(0.2); //evaluated at compile time?

double myVariable = 0.3;
const double result3 = RAD2DEG_expr(myVariable); //ok - but can only be evaluated at run time

const double myOtherVariable = 0.4;
const double result4 = RAD2DEG_expr(myOtherVariable); //ok - evaluated at compile time because parameter is CONST?


I found also constexpr function and its parameter and constexpr vs const vs constexpr const .










share|improve this question

























  • What's the question? You saw that acosdidn't work within a constexpr.

    – Matthieu Brucher
    Nov 21 '18 at 15:17













  • @MatthieuBrucher: Pi_cnst cannot be constexpr because of acos but RAD2DEG_cnst is constexpr although it relies on Pi_cnst. I believe the question is: how can it be declared as constexpr while performing unconstexprable operations, and what does it mean in practice about compile-time evaluation?

    – papagaga
    Nov 21 '18 at 15:21











  • Oh, OK. I think constexpr doesn't always been a build time...

    – Matthieu Brucher
    Nov 21 '18 at 15:23











  • I get an error for RAD2DEG_cnst with gcc9 though: "In function 'constexpr double RAD2DEG_cnst(double)': error: the value of 'Pi_cnst' is not usable in a constant expression"

    – papagaga
    Nov 21 '18 at 15:23






  • 1





    Possible duplicate of When does a constexpr function get evaluated at compile time?

    – Matthieu Brucher
    Nov 21 '18 at 15:23
















1















Please can someone clarify (I'm using Visual Studio 15.9.2):



In the following code given that Pi_cnst is evaluated at run time (because defining Pi in this way requires a run time calculation), will RAD2DEG_cnst ever be evaluated at compile time or is the use of "constexpr" always reverting to "const" ?



edit - add: If it is always reverting to const then should I expect a warning, or is it bad in some other way i.e. it seems odd to be able to so easily declare a constexpr for the body to be accepted but ALWAYS be such that it is never actually a constexpr. What have I missed?



constexpr double Pi_error = std::acos(-1.0); //error function call must have a constant value in a constant expression (does not compile)
const double Pi_cnst = std::acos(-1.0); //ok evaluated at run time

constexpr double Pi_expr = 3.1415926; //ok valid constexpr

constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; } //no error or warning BUT is this ever evaluated at compile time?
constexpr double RAD2DEG_expr(double rad) { return rad * 180.0 / Pi_expr; } //ok can be evaluated at compile time

const double result1 = RAD2DEG_cnst(0.1); //evaluated at run time?
const double result2 = RAD2DEG_expr(0.2); //evaluated at compile time?

double myVariable = 0.3;
const double result3 = RAD2DEG_expr(myVariable); //ok - but can only be evaluated at run time

const double myOtherVariable = 0.4;
const double result4 = RAD2DEG_expr(myOtherVariable); //ok - evaluated at compile time because parameter is CONST?


I found also constexpr function and its parameter and constexpr vs const vs constexpr const .










share|improve this question

























  • What's the question? You saw that acosdidn't work within a constexpr.

    – Matthieu Brucher
    Nov 21 '18 at 15:17













  • @MatthieuBrucher: Pi_cnst cannot be constexpr because of acos but RAD2DEG_cnst is constexpr although it relies on Pi_cnst. I believe the question is: how can it be declared as constexpr while performing unconstexprable operations, and what does it mean in practice about compile-time evaluation?

    – papagaga
    Nov 21 '18 at 15:21











  • Oh, OK. I think constexpr doesn't always been a build time...

    – Matthieu Brucher
    Nov 21 '18 at 15:23











  • I get an error for RAD2DEG_cnst with gcc9 though: "In function 'constexpr double RAD2DEG_cnst(double)': error: the value of 'Pi_cnst' is not usable in a constant expression"

    – papagaga
    Nov 21 '18 at 15:23






  • 1





    Possible duplicate of When does a constexpr function get evaluated at compile time?

    – Matthieu Brucher
    Nov 21 '18 at 15:23














1












1








1


1






Please can someone clarify (I'm using Visual Studio 15.9.2):



In the following code given that Pi_cnst is evaluated at run time (because defining Pi in this way requires a run time calculation), will RAD2DEG_cnst ever be evaluated at compile time or is the use of "constexpr" always reverting to "const" ?



edit - add: If it is always reverting to const then should I expect a warning, or is it bad in some other way i.e. it seems odd to be able to so easily declare a constexpr for the body to be accepted but ALWAYS be such that it is never actually a constexpr. What have I missed?



constexpr double Pi_error = std::acos(-1.0); //error function call must have a constant value in a constant expression (does not compile)
const double Pi_cnst = std::acos(-1.0); //ok evaluated at run time

constexpr double Pi_expr = 3.1415926; //ok valid constexpr

constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; } //no error or warning BUT is this ever evaluated at compile time?
constexpr double RAD2DEG_expr(double rad) { return rad * 180.0 / Pi_expr; } //ok can be evaluated at compile time

const double result1 = RAD2DEG_cnst(0.1); //evaluated at run time?
const double result2 = RAD2DEG_expr(0.2); //evaluated at compile time?

double myVariable = 0.3;
const double result3 = RAD2DEG_expr(myVariable); //ok - but can only be evaluated at run time

const double myOtherVariable = 0.4;
const double result4 = RAD2DEG_expr(myOtherVariable); //ok - evaluated at compile time because parameter is CONST?


I found also constexpr function and its parameter and constexpr vs const vs constexpr const .










share|improve this question
















Please can someone clarify (I'm using Visual Studio 15.9.2):



In the following code given that Pi_cnst is evaluated at run time (because defining Pi in this way requires a run time calculation), will RAD2DEG_cnst ever be evaluated at compile time or is the use of "constexpr" always reverting to "const" ?



edit - add: If it is always reverting to const then should I expect a warning, or is it bad in some other way i.e. it seems odd to be able to so easily declare a constexpr for the body to be accepted but ALWAYS be such that it is never actually a constexpr. What have I missed?



constexpr double Pi_error = std::acos(-1.0); //error function call must have a constant value in a constant expression (does not compile)
const double Pi_cnst = std::acos(-1.0); //ok evaluated at run time

constexpr double Pi_expr = 3.1415926; //ok valid constexpr

constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; } //no error or warning BUT is this ever evaluated at compile time?
constexpr double RAD2DEG_expr(double rad) { return rad * 180.0 / Pi_expr; } //ok can be evaluated at compile time

const double result1 = RAD2DEG_cnst(0.1); //evaluated at run time?
const double result2 = RAD2DEG_expr(0.2); //evaluated at compile time?

double myVariable = 0.3;
const double result3 = RAD2DEG_expr(myVariable); //ok - but can only be evaluated at run time

const double myOtherVariable = 0.4;
const double result4 = RAD2DEG_expr(myOtherVariable); //ok - evaluated at compile time because parameter is CONST?


I found also constexpr function and its parameter and constexpr vs const vs constexpr const .







c++ constexpr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 15:21







Ben Senior

















asked Nov 21 '18 at 15:15









Ben SeniorBen Senior

85




85













  • What's the question? You saw that acosdidn't work within a constexpr.

    – Matthieu Brucher
    Nov 21 '18 at 15:17













  • @MatthieuBrucher: Pi_cnst cannot be constexpr because of acos but RAD2DEG_cnst is constexpr although it relies on Pi_cnst. I believe the question is: how can it be declared as constexpr while performing unconstexprable operations, and what does it mean in practice about compile-time evaluation?

    – papagaga
    Nov 21 '18 at 15:21











  • Oh, OK. I think constexpr doesn't always been a build time...

    – Matthieu Brucher
    Nov 21 '18 at 15:23











  • I get an error for RAD2DEG_cnst with gcc9 though: "In function 'constexpr double RAD2DEG_cnst(double)': error: the value of 'Pi_cnst' is not usable in a constant expression"

    – papagaga
    Nov 21 '18 at 15:23






  • 1





    Possible duplicate of When does a constexpr function get evaluated at compile time?

    – Matthieu Brucher
    Nov 21 '18 at 15:23



















  • What's the question? You saw that acosdidn't work within a constexpr.

    – Matthieu Brucher
    Nov 21 '18 at 15:17













  • @MatthieuBrucher: Pi_cnst cannot be constexpr because of acos but RAD2DEG_cnst is constexpr although it relies on Pi_cnst. I believe the question is: how can it be declared as constexpr while performing unconstexprable operations, and what does it mean in practice about compile-time evaluation?

    – papagaga
    Nov 21 '18 at 15:21











  • Oh, OK. I think constexpr doesn't always been a build time...

    – Matthieu Brucher
    Nov 21 '18 at 15:23











  • I get an error for RAD2DEG_cnst with gcc9 though: "In function 'constexpr double RAD2DEG_cnst(double)': error: the value of 'Pi_cnst' is not usable in a constant expression"

    – papagaga
    Nov 21 '18 at 15:23






  • 1





    Possible duplicate of When does a constexpr function get evaluated at compile time?

    – Matthieu Brucher
    Nov 21 '18 at 15:23

















What's the question? You saw that acosdidn't work within a constexpr.

– Matthieu Brucher
Nov 21 '18 at 15:17







What's the question? You saw that acosdidn't work within a constexpr.

– Matthieu Brucher
Nov 21 '18 at 15:17















@MatthieuBrucher: Pi_cnst cannot be constexpr because of acos but RAD2DEG_cnst is constexpr although it relies on Pi_cnst. I believe the question is: how can it be declared as constexpr while performing unconstexprable operations, and what does it mean in practice about compile-time evaluation?

– papagaga
Nov 21 '18 at 15:21





@MatthieuBrucher: Pi_cnst cannot be constexpr because of acos but RAD2DEG_cnst is constexpr although it relies on Pi_cnst. I believe the question is: how can it be declared as constexpr while performing unconstexprable operations, and what does it mean in practice about compile-time evaluation?

– papagaga
Nov 21 '18 at 15:21













Oh, OK. I think constexpr doesn't always been a build time...

– Matthieu Brucher
Nov 21 '18 at 15:23





Oh, OK. I think constexpr doesn't always been a build time...

– Matthieu Brucher
Nov 21 '18 at 15:23













I get an error for RAD2DEG_cnst with gcc9 though: "In function 'constexpr double RAD2DEG_cnst(double)': error: the value of 'Pi_cnst' is not usable in a constant expression"

– papagaga
Nov 21 '18 at 15:23





I get an error for RAD2DEG_cnst with gcc9 though: "In function 'constexpr double RAD2DEG_cnst(double)': error: the value of 'Pi_cnst' is not usable in a constant expression"

– papagaga
Nov 21 '18 at 15:23




1




1





Possible duplicate of When does a constexpr function get evaluated at compile time?

– Matthieu Brucher
Nov 21 '18 at 15:23





Possible duplicate of When does a constexpr function get evaluated at compile time?

– Matthieu Brucher
Nov 21 '18 at 15:23












1 Answer
1






active

oldest

votes


















3














Your code is ill-formed, no diagnostic required. In



constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; }


There is no input which will ever make the function a core constant expression which is specified by [dcl.constexpr]/5




For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, a constant initializer for some object ([basic.start.static]), the program is ill-formed, no diagnostic required.







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%2f53415120%2fconstexpr-function-contains-a-const-will-it-ever-be-evaluated-at-compile-time%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Your code is ill-formed, no diagnostic required. In



    constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; }


    There is no input which will ever make the function a core constant expression which is specified by [dcl.constexpr]/5




    For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, a constant initializer for some object ([basic.start.static]), the program is ill-formed, no diagnostic required.







    share|improve this answer




























      3














      Your code is ill-formed, no diagnostic required. In



      constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; }


      There is no input which will ever make the function a core constant expression which is specified by [dcl.constexpr]/5




      For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, a constant initializer for some object ([basic.start.static]), the program is ill-formed, no diagnostic required.







      share|improve this answer


























        3












        3








        3







        Your code is ill-formed, no diagnostic required. In



        constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; }


        There is no input which will ever make the function a core constant expression which is specified by [dcl.constexpr]/5




        For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, a constant initializer for some object ([basic.start.static]), the program is ill-formed, no diagnostic required.







        share|improve this answer













        Your code is ill-formed, no diagnostic required. In



        constexpr double RAD2DEG_cnst(double rad) { return rad * 180.0 / Pi_cnst; }


        There is no input which will ever make the function a core constant expression which is specified by [dcl.constexpr]/5




        For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, or, for a constructor, a constant initializer for some object ([basic.start.static]), the program is ill-formed, no diagnostic required.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 15:23









        NathanOliverNathanOliver

        95.6k16135208




        95.6k16135208
































            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%2f53415120%2fconstexpr-function-contains-a-const-will-it-ever-be-evaluated-at-compile-time%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

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?