2

Possible Duplicate:
declaring a const instance of a class
Why does C++ require a user-provided default constructor to default-construct a const object?

My program like this:

class c
{
};

int main()
{
    const c a;

    return 0;
}

when I compile it using g++, it prompt:

main.cpp:10:7: note: ‘const class c’ has no user-provided default constructor

Why, this is just an empty class and do not do anything, why I have to provide a user-provided constructor.

Community
  • 1
  • 1
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
  • 3
    See http://stackoverflow.com/questions/5934457/why-does-const-instance-require-hand-made-default-constructor-gcc-4-2-1 and http://stackoverflow.com/questions/4674332/declaring-a-const-instance-of-a-class – juanchopanza Mar 09 '12 at 10:11
  • 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. Otherwise, if no initializer is specified for an object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed. $8.5/9 – Prasoon Saurav Mar 09 '12 at 10:14

2 Answers2

1

Because the language rules say so.

The constant must have its value set in the definition, as it cannot be assigned a value later. If you don't explicitly provide a value, the type must have a default constructor.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
1

You don't have to. It's just a note, not even a warning. The rationale is that the class can't do anything useful, which is rarely intended. GCC is just checking to see if you overlooked something.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • GCC is wrong. This should be a compile error (and in fact my version of GCC (`llvm-g++-4.2`) *does* issue an error). – Konrad Rudolph Mar 09 '12 at 10:14
  • clang++ doesn't issue any error which is wrong. My g++4.6 issues error: uninitialized const ‘a’ [-fpermissive] – Prasoon Saurav Mar 09 '12 at 10:16
  • @Prasoon Which version of `clang++` are you using? My version (3.0) correctly treats this as an error. Unfortunately, `g++-4.6.2` doesn’t. Which is weird, since your GCC 4.6 does. – Konrad Rudolph Mar 09 '12 at 10:18
  • 3
    @KonradRudolph: It's a language extension; it doesn't impact conforming code. There is a diagnostic, as required. Overall, I wouldn't call it wrong. – MSalters Mar 09 '12 at 10:20
  • @MSalters I’m pretty sure that the program is ill-formed and it’s thus wrong. Unfortunately I cannot find the passage cited by Prasoon but either way, you end up with an uninitialised `const` object and that *should not* be legal. That said, C++ also allows other uninitialised PODs to exist and that too should actually be illegal since otherwise you’ll be forced to call `operator =` on an uninitialised value. *EDIT* and if it’s a language extension then GCC is *still* wrong since some compiler versions don’t issue *any* diagnostic, not even when compiled with `-pedantic`. – Konrad Rudolph Mar 09 '12 at 10:27
  • Well, an uninitialized _empty_ object is not a big deal IMO. I agree that a diagnostic is necessarily, certainly with `-pedantic`. Do file a bug report, it looks like a regression. I could imagine GCC code that checks if there are any uninitialized members in a `const` object, overlooking this special case. – MSalters Mar 09 '12 at 10:33
  • Answering my previous comment myself: if a class overloads `operator =` it’s no longer a POD so this case doesn’t pose any problems. So in summary, the C++ standard is consistent. – Konrad Rudolph Mar 09 '12 at 10:33
  • Ugh, I hate filing bugs with GCC. :-( – Konrad Rudolph Mar 09 '12 at 10:34
  • @KonradRudolph : I am using 2.9 version – Prasoon Saurav Mar 09 '12 at 10:42
  • @MSalters [Bug report filed](http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52541). By the way, I think OP truncated the error message. The full message *does* include an error, the note is merely explanatory. – Konrad Rudolph Mar 09 '12 at 11:10