Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Default constructor and const

3 views
Skip to first unread message

mathieu

unread,
Jan 21, 2010, 9:10:45 AM1/21/10
to
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".

Company

unread,
Jan 21, 2010, 9:27:24 AM1/21/10
to
Il se trouve que mathieu a formulᅵ :

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


Alf P. Steinbach

unread,
Jan 21, 2010, 9:36:21 AM1/21/10
to
* 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

Niels Dekker - no reply address

unread,
Jan 21, 2010, 10:50:29 AM1/21/10
to
Mathieu wrote:
> 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
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center


Saeed Amrollahi

unread,
Jan 21, 2010, 12:54:33 PM1/21/10
to
> - 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

Daniel Giaimo

unread,
Jan 21, 2010, 1:07:07 PM1/21/10
to
>> 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.

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

--
Dan Giaimo

Jerry Coffin

unread,
Jan 21, 2010, 2:53:38 PM1/21/10
to
In article <2ef6b44b-6672-4018-8cc6-
e93ef7...@c29g2000yqd.googlegroups.com>,
mathieu....@gmail.com 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.

--
Later,
Jerry.

Pete Becker

unread,
Jan 21, 2010, 2:58:35 PM1/21/10
to

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

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

Saeed Amrollahi

unread,
Jan 21, 2010, 3:22:09 PM1/21/10
to

Thanks Pete.
-- Saeed

Daniel Giaimo

unread,
Jan 21, 2010, 3:46:54 PM1/21/10
to
On 1/21/2010 2:58 PM, Pete Becker wrote:
> Daniel Giaimo wrote:
>> On 1/21/2010 12:54 PM, Saeed Amrollahi wrote:
>>> 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.
>>
>
> 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.

--
Dan Giaimo

Bo Persson

unread,
Jan 21, 2010, 5:14:50 PM1/21/10
to
Niels Dekker - no reply address wrote:
> Mathieu wrote:
>> 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?
>

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

How do you get something less useful? :-)


Bo Persson


Pete Becker

unread,
Jan 21, 2010, 5:42:12 PM1/21/10
to
Daniel Giaimo wrote:
> On 1/21/2010 2:58 PM, Pete Becker wrote:
>> Daniel Giaimo wrote:
>>> On 1/21/2010 12:54 PM, Saeed Amrollahi wrote:
>>>> 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.
>>>
>>
>> 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:
>

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.

Daniel Giaimo

unread,
Jan 21, 2010, 7:27:01 PM1/21/10
to
On 1/21/2010 5:42 PM, Pete Becker wrote:
> Daniel Giaimo wrote:
>> On 1/21/2010 2:58 PM, Pete Becker wrote:
>>> Daniel Giaimo wrote:
>>>> On 1/21/2010 12:54 PM, Saeed Amrollahi wrote:
>>>>> 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.
>>>>
>>>
>>> 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:
>>
>
> 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.

--
Dan Giaimo

mathieu

unread,
Jan 22, 2010, 6:23:35 AM1/22/10
to

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

Pete Becker

unread,
Jan 22, 2010, 7:23:36 AM1/22/10
to

That's true. But the point under discussion in this subthread is whether
"The C++ standard would disagree with you...". See above.

> In order to be a valid C++ program _any_ conforming compiler
> must be able to compile it.

For some unspecified value of "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.

The standard says that this code is ill-formed. If you're not happy with
getting a warning for it, then set your compiler to treat warnings as
errors. If you don't configure your compiler to do what you want, don't
complain that the compiler isn't doing what you want it to do.

Yu Han

unread,
Jan 23, 2010, 1:19:02 PM1/23/10
to
What bug?

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

James Kanze

unread,
Jan 23, 2010, 6:55:10 PM1/23/10
to
On Jan 23, 6:19 pm, Yu Han <hanju...@163.com> wrote:
> On 01/22/2010 07:23 PM, mathieu wrote:

> > On Jan 22, 1:27 am, Daniel Giaimo<dgia...@gmail.com> wrote:
> >> On 1/21/2010 5:42 PM, Pete Becker wrote:

[...]


> > 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;
> > }

> 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.

--
James Kanze

Branimir Maksimovic

unread,
Jan 23, 2010, 7:56:14 PM1/23/10
to
James Kanze wrote:
> On Jan 23, 6:19 pm, Yu Han <hanju...@163.com> wrote:
>>> 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;
>>> }
>
>> What bug?
>
>
> 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

mathieu

unread,
Jan 25, 2010, 4:36:56 AM1/25/10
to

Exactly what I am having here. This has been logged as:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42844

Cheers

Vladimir Jovic

unread,
Jan 25, 2010, 4:48:34 AM1/25/10
to

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.

Alf P. Steinbach

unread,
Jan 25, 2010, 7:42:30 AM1/25/10
to
* Vladimir Jovic:

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.


>> Explicitly defining a default constructor is one way.
>>
>
> Not if it is a POD.

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

0 new messages