0

in my c++ book this is written and there is an error on the second new_sz() that says:function call must have a constant value in a constant expression.

constexpr int new_sz(){return 42;}
constexpr int foo=new_sz();  //ok:foo is a constant expression.

but when it is:

constexpr int new_sz(){return 42;}
const int foo=new_sz();

there is no error . what I want to know is that do constexpr an const differ? if so: how can I deal with that error? it is said in my book: here we defined new_sz as a constexpr.the compiler can verify_at compile time_that a call to new_sz returns a constant expression, so we can use new_sz to initialize our constexpre variable , foo. but there is still an error although in book it is right.

saeed abbasi
  • 83
  • 10
  • The const in your second function declaration is completely superfluous as prvalues of const scalar type are adjusted to have non-const type (as described by [expr]/6). – Columbo Apr 11 '15 at 09:49
  • Consider `const int f() {return time(NULL);}` vs `constexpr int f() {return time(NULL);}` – user253751 Apr 11 '15 at 10:23

0 Answers0