constexpr function contains a const - will it ever be evaluated at compile time?
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
|
show 1 more comment
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
What's the question? You saw thatacosdidn't work within a constexpr.
– Matthieu Brucher
Nov 21 '18 at 15:17
@MatthieuBrucher:Pi_cnstcannot beconstexprbecause ofacosbutRAD2DEG_cnstisconstexpralthough it relies onPi_cnst. I believe the question is: how can it be declared asconstexprwhile 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 forRAD2DEG_cnstwith 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
|
show 1 more comment
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
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
c++ constexpr
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 thatacosdidn't work within a constexpr.
– Matthieu Brucher
Nov 21 '18 at 15:17
@MatthieuBrucher:Pi_cnstcannot beconstexprbecause ofacosbutRAD2DEG_cnstisconstexpralthough it relies onPi_cnst. I believe the question is: how can it be declared asconstexprwhile 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 forRAD2DEG_cnstwith 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
|
show 1 more comment
What's the question? You saw thatacosdidn't work within a constexpr.
– Matthieu Brucher
Nov 21 '18 at 15:17
@MatthieuBrucher:Pi_cnstcannot beconstexprbecause ofacosbutRAD2DEG_cnstisconstexpralthough it relies onPi_cnst. I believe the question is: how can it be declared asconstexprwhile 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 forRAD2DEG_cnstwith 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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 15:23
NathanOliverNathanOliver
95.6k16135208
95.6k16135208
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the question? You saw that
acosdidn't work within a constexpr.– Matthieu Brucher
Nov 21 '18 at 15:17
@MatthieuBrucher:
Pi_cnstcannot beconstexprbecause ofacosbutRAD2DEG_cnstisconstexpralthough it relies onPi_cnst. I believe the question is: how can it be declared asconstexprwhile 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_cnstwith 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