This is related to http://gcc.gnu.org/ml/gcc/2010-11/msg00623.html

I write about it again because the following seems too bad:

$ cat test1.cc 
struct X {
  X()=default;
  X(X&&)=default;
  X(X const&)=delete;
  //some very large or non-copyable content
};

X test() {
  X const x={};
  {
    //a lot of code where I do not want to modify x [accidentally]
  }
  return x;
}

$ g++ -c -std=c++0x test1.cc 
test1.cc: In function 'X test()':
test1.cc:13:10: error: use of deleted function 'X::X(const X&)'
test1.cc:4:3: error: declared here

Another related example:

$ cat test2.cc 
struct U {
  U();
  U(U&&);
  U(U const&);
};

struct X {
  U const u;
  X()=default;
  X(X&&)=default;
  //100 other members
};

X test() {
  X a={};
  return a;
}

$ g++ -c -std=c++0x test2.cc 
test2.cc: In function 'X test()':
test2.cc:16:10: error: use of deleted function 'X::X(X&&)'
test2.cc:10:3: error: 'X::X(X&&)' is implicitly deleted because the default 
definition would be ill-formed:
test2.cc:10:3: error: non-static data member 'X::u' does not have a move 
constructor or trivial copy constructor

In both examples, g++0x forces removing "const" (in "X const x={};" and
"U const u;") or doing some other ugly things like const_cast. In
general, it is not good to discourage using "const" for a number of
reasons. Additionally, if I have to have constness, I need to add
additional clutter reducing readability.

Can nothing be done to preserve "const"? Does the Standard really make
this code invalid? Gcc v4.5 was friendlier in this respect.

Reply via email to