This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/12226] New: g++ fails to enforce accessibility requirement for copy constructor
- From: "austern at apple dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 9 Sep 2003 17:46:06 -0000
- Subject: [Bug c++/12226] New: g++ fails to enforce accessibility requirement for copy constructor
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12226
Summary: g++ fails to enforce accessibility requirement for copy
constructor
Product: gcc
Version: 3.4
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: austern at apple dot com
CC: gcc-bugs at gcc dot gnu dot org,gdr at integrable-
solutions dot net
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
Consider the following code snippet, which is very similar to an example near the end of section
8.5.3 in the C++ standard.
struct A { protected: A(const A&); };
struct B : public A { public: B(const B&); };
extern B f();
const A& ra = f();
Even with -W -Wall, this compiles cleanly with no diagnostics.
This is wrong. This example is ill-formed, and a diagnostic is required. A's copy constructor is not
accessible in the initialization of ra, and the standard requires it to be. From section 8.5.3,
paragraph 5: "If the initializer expression is an rvalue, with T2 a class type, and cv1 T1 is
reference-compatible with cv2 T2, the reference is bound in one of the following ways [description
of the two ways omitted] ... The constructor that would be used to make the copy shall be callable
whether or not the copy is actually done."
I believe that http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3471 was an incorrect fix, and that it
disabled some correct diagnostics. Accessibility checking is required whether or not the copy
constructor is invoked, because eliding the copy constructor is just an optimization and
correctness of a program does not depend on how smart a compiler's optimizer happens to be.