mardi 3 mai 2016

why rvalue can't assign to constexpr reference variable

I have the following code

using namespace std;

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. Like const, 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. I thought constexpr can be replaced in the places where we need const butfor the above code I am getting 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 couldn't get this behavior can somebody shed some light.

Aucun commentaire:

Enregistrer un commentaire