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.