I have the following code
constexpr int into(int a,int b)
{
int c=a*b;
return c;
}
int main()
{
constexpr int &n=into(5,5);
}
and I have read (in MSDN)
The keyword
constexpr
was introduced in C++11 and improved in C++14. It means constant expression. Likeconst
, it can be applied to variables so that a compiler error will be raised if any code attempts to modify the value.
After I read it, I thought that constexpr
can be used in place of const
, but for the above code I get a compiler error stating
`int main()': invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'`
When constexpr
is replaced with const
, it works fine. I don't understand this behavior; can somebody shed some light?