Default constructor and const

M

mathieu

Hi there,

Could someone let me know why Comeau reports an error on the
following code. The compiler should provide a default constuctor for
me.

struct A { int I; };
int main()
{
const A a;
return 0;
}

Thanks !

Output:
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA2
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ noC++0x_extensions

"ComeauTest.c", line 8: error: const variable "a" requires an
initializer -- class
"A" has no explicitly declared default constructor
const A a;
^

"ComeauTest.c", line 8: warning: variable "a" was declared but never
referenced
const A a;
^

1 error detected in the compilation of "ComeauTest.c".
 
C

Company

Il se trouve que mathieu a formulé :
Hi there,

Could someone let me know why Comeau reports an error on the
following code. The compiler should provide a default constuctor for
me.

struct A { int I; };
int main()
{
const A a;
return 0;
}

Thanks !

Output:
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA2
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ noC++0x_extensions

"ComeauTest.c", line 8: error: const variable "a" requires an
initializer -- class
"A" has no explicitly declared default constructor
const A a;
^

"ComeauTest.c", line 8: warning: variable "a" was declared but never
referenced
const A a;
^

1 error detected in the compilation of "ComeauTest.c".

A constant data member can only be initialized in the initializer list
of a constructor.
 
A

Alf P. Steinbach

* mathieu:
Hi there,

Could someone let me know why Comeau reports an error on the
following code. The compiler should provide a default constuctor for
me.

struct A { int I; };
int main()
{
const A a;
return 0;
}

Thanks !

Output:
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA2
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ noC++0x_extensions

"ComeauTest.c", line 8: error: const variable "a" requires an
initializer -- class
"A" has no explicitly declared default constructor
const A a;
^

"ComeauTest.c", line 8: warning: variable "a" was declared but never
referenced
const A a;
^

1 error detected in the compilation of "ComeauTest.c".

The C++ rules amount to this: that a constant's value must be explicitly
specified in some way.

Explicitly defining a default constructor is one way.


Cheers & hth.,

- Alf
 
N

Niels Dekker - no reply address

Mathieu said:
Could someone let me know why Comeau reports an error on the
following code. The compiler should provide a default constuctor for
me.

struct A { int I; };
int main()
{
const A a;
return 0;
}

What do you want? Do you want your a.I to be zero-initialized, or do you
want it to have an indeterminate value?

Kind regards,

Niels
 
S

Saeed Amrollahi

* mathieu:













The C++ rules amount to this: that a constant's value must be explicitly
specified in some way.

Explicitly defining a default constructor is one way.

Cheers & hth.,

- Alf- Hide quoted text -

- Show quoted text -

Hi Alf
What about the -implicit- default constructor?
I ran the program under MS Visual Studio 2008. It issued a warning:
'a' : 'const' automatic data initialized with compiler generated
default
constructor produces unreliable results.
I think it is more reasonable. BTW, it is not invalid program.

Regards,
-- Saeed Amrollahi
 
D

Daniel Giaimo

Hi Alf
What about the -implicit- default constructor?
I ran the program under MS Visual Studio 2008. It issued a warning:
'a' : 'const' automatic data initialized with compiler generated
default
constructor produces unreliable results.
I think it is more reasonable. BTW, it is not invalid program.

The C++ standard would disagree with you. The fact that M$
Visual Studio allows it does not mean it is a valid C++
program.
 
J

Jerry Coffin

(e-mail address removed)>,
(e-mail address removed) says...
Hi there,

Could someone let me know why Comeau reports an error on the
following code. The compiler should provide a default constuctor for
me.

struct A { int I; };
int main()
{
const A a;
return 0;
}

You haven't initialized 'a.I', but anything that's const must be
initialized. As it is now, there's almost nothing you can safely do
with a.I -- in fact, the only thing I can think of is cast its
address to a pointer to unsigned char, and read the raw bytes.
 
D

Daniel Giaimo

In particular, the standard requires "a diagnostic". A warning can be a
diagnostic if the compiler's documentation says it is.

I'm not sure what you're saying here. Are you saying that this is a
valid C++ program? I don't have a copy of the final C++ standard on me
right now, but a quick search for a working draft online yields the
following:

Section 8.5 Paragraph 9:

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 a
non-static 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.

In the OP's case, the relevant class is of POD type, so the second half
of this paragraph applies. In particular the sentence "Otherwise, if
no initializer is specified for a non-static 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_." applies. (Underline mine) This seems pretty
clear.
 
B

Bo Persson

Niels said:
What do you want? Do you want your a.I to be zero-initialized, or
do you want it to have an indeterminate value?

Yes, it has a constant value that cannot be read.

How do you get something less useful? :)


Bo Persson
 
D

Daniel Giaimo

If MS says that warnings are diagnostics, then Visual Studio's handling
of that code conforms to the standard. The fact that it then "allows" it
is irrelevant.

Whether Visual Studio's handling of the code conforms to the standard
is irrelevant to the question of whether the program is a valid C++
program. In order to be a valid C++ program _any_ conforming compiler
must be able to compile it. The program the OP posted fails this test
as the standard allows this code to be rejected as ill-formed which,
as the OP noted, is what the Comeau compiler does.
 
M

mathieu

Whether Visual Studio's handling of the code conforms to the standard
is irrelevant to the question of whether the program is a valid C++
program.  In order to be a valid C++ program _any_ conforming compiler
must be able to compile it.  The program the OP posted fails this test
as the standard allows this code to be rejected as ill-formed which,
as the OP noted, is what the Comeau compiler does.

Thanks everyone for the comments.

In this case I think I found a bug in g++4.5:


struct A
{
A(){}
};

struct B : public A
{
};


int main()
{
const B b;
return 0;
}

Cheers
 
J

James Kanze

On 01/22/2010 07:23 PM, mathieu wrote:

[...]
What bug?

Presumably (from the thread) that the code compiles without even
a warning. I don't have a copy of the standard handy, to see if
there is any exception to allow this. (But from memory, I think
the above code does require a diagnostic.) And I don't have g++
installed on this machine to see if it really compiles when g++
is invoked as a standard compliant C++ compiler; e.g. with
-std=c++98 and such.)

FWIW: if memory serves me correctly, even something like:

struct A{};
A const a;

requires a diagnostic.
 
B

Branimir Maksimovic

James said:
FWIW: if memory serves me correctly, even something like:

struct A{};
A const a;

requires a diagnostic.

Comeau online says error, it reqires default constructor or initializer
while g++ 4.4.1 compiles without warning or error even with -std=c++98
-pedantic -Wall

In case of struct A{}; A const a;
g++ gives error about non initialized constant.

Greets
 
V

Vladimir Jovic

Alf said:
* mathieu:

The C++ rules amount to this: that a constant's value must be explicitly
specified in some way.

In this case, like this :

struct A { int I; };
int main()
{
const A a = { 3 };
}
Explicitly defining a default constructor is one way.

Not if it is a POD.
 
A

Alf P. Steinbach

* Vladimir Jovic:
In this case, like this :

struct A { int I; };

OK, it's just repeating the OP's struct definition, but as a general rule, don't
use all uppercase names for anything but macros, except where they're idiomatic
(as in a template parameter T).

int main()
{
const A a = { 3 };
}

Usually there isn't just one way to do something.

For example,

A x; // Statically initialized to zero
int main()
{
A const a = x;
}

or

int main()
{
A const a = A();
}

Or one might get more elaborate and use a function returning an A, with that
function e.g. constructing an A from raw bytes.

Not if it is a POD.

I think you mean, not if it is required to be a POD. ;-)


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top