[LLVMbugs] [Bug 6772] Not an error for gcc -- error: default initialization of an object of const type '...' requires a user-provided default constructor

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun Apr 4 02:15:35 PDT 2010


http://llvm.org/bugs/show_bug.cgi?id=6772

Chandler Carruth <chandlerc at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |chandlerc at gmail.com
         Resolution|                            |INVALID

--- Comment #1 from Chandler Carruth <chandlerc at gmail.com> 2010-04-04 04:15:34 CDT ---
(In reply to comment #0)
> Script started on Sat Apr  3 16:29:45 2010
> $ cat ccd.cpp
> struct A {
>         A() {}
> };
> 
> class B {
>         A a_;
> };
> 
> int
> foo()
> {
>         const B b;
>         return 0;
> }
> $ g++ --version
> g++ (GCC) 4.2.1 20070719  [FreeBSD]
> Copyright (C) 2007 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> 
> $ g++ -c ccd.cpp
> $ clang++ --version
> clang version 1.5 (trunk)
> Target: i386-portbld-freebsd9.0
> Thread model: posix
> $ clang++ -c ccd.cpp
> ccd.cpp:12:10: error: default initialization of an object of const type 'B
> const' requires a user-provided default constructor

Clang is correct: C++'03 [dcl.init] p9: If no initializer is specified for an
object, and the object is of (possibly cv-qualified) non-POD class type (or
array thereof), the object shall be default-initialized; if the object is of
const-qualified type, the underlying class type shall have a user-declared
default constructor.

The user declared default constructor for A makes A non-POD. The A member in B
makes B non-POD, and thus this rule kicks in, and the code provided is invalid.

You could re-open as a request for a GCC-compatibility setting to allow such
code, but I don't personally see a lot of value in it. Perhaps others do, so
feel free to re-open if you want to pursue that path.

>         const B b;
>                 ^
> 1 diagnostic generated.
> $ ^D
> Script done on Sat Apr  3 16:30:29 2010

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list