Adding extra constness causes compiler error

Because returning a const something by value like here makes no difference with or without.

For example:

const int GetMyInt()
{
  int k = 42;
  return k;
}

//later..
int ret = GetMyInt();
// modify ret.

Because the returned value from GetMyInt will be copied into ret anyway (not taking (N)RVO into account), having GetMyInt return const makes no difference.

Normally this is a warning because it's superfluous code but -Werror turns every warning into an error so there's that.


The const qualifier has no effect in this position, since the returned value is a prvalue of non-class type and therefore cannot be modified anyway.

Notice that the compiler message says -Werror=, meaning that it's normally a warning (so the code is not wrong, but warning-worthy). It has been turned into an error by your compilation settings.

Tags:

C++