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

Very weird compiler bug or normal ISO C++ behaviour?

1 view
Skip to first unread message

fabio.b...@removethistomailmeomega64.com

unread,
Sep 11, 2006, 12:01:15 PM9/11/06
to

Hello all,
I went across what seems possibly a bug of the compiler (VisualC 2005,
just for the record) or a very strange and non-expected (by me at least)
behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.

Here we go: I noticed that if I nest a structure inside another one,
the wrong constructor will be called.. the one from a different class!!

Here is some test code:

---

struct One {
int o;
One() {
printf("Into the constructor of One which is inherited by ");
};
};

struct Two : One {
int t;
Two() {
printf("Two\n");
};
};

struct Zwei : One {
int z;
Zwei() {
printf("Zwei\n");
};
};

struct Dos : One {
int d;
Dos() {
printf("Dos\n");
};
};

struct MyFinalStruct_this_acts_bug_free {
Two MyObject1;
Zwei MyObject2;
Dos MyObject3;
} ;

struct MyFinalStruct_this_produces_the_bug {
struct {
Two MyObject1;
} A;
struct {
Zwei MyObject2;
} B;
struct {
Dos MyObject3;
} C;
} ;

int main() {
printf("...testing the first structure:\n");
MyFinalStruct_this_acts_bug_free Test1;
printf("...testing the second structure:\n");
MyFinalStruct_this_produces_the_bug Test2;
// --
return(0);
}

---

The above program produces this output, at least on my compiler:

---

...testing the first structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos
...testing the second structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Two

---

Is this normal? The first output is what I would definitely expect
also from the other structure!

Thanks,
Fabio

Clark S. Cox III

unread,
Sep 11, 2006, 12:14:39 PM9/11/06
to
fabio.b...@REMOVETHISTOMAILMEomega64.com wrote:
> Hello all,
> I went across what seems possibly a bug of the compiler (VisualC 2005,
> just for the record) or a very strange and non-expected (by me at least)
> behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.

[snip code]

> Is this normal? The first output is what I would definitely expect
> also from the other structure!

It appears to be a bug. After adding the needed

"#include <cstdio>
using namespace std"

, GCC produces the following output:

[ccox-macbook:~] ccox% g++ test.cpp && ./a.out


...testing the first structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos
...testing the second structure:
Into the constructor of One which is inherited by Two
Into the constructor of One which is inherited by Zwei
Into the constructor of One which is inherited by Dos

--
Clark S. Cox III
clar...@gmail.com

Ulrich Eckhardt

unread,
Sep 11, 2006, 12:19:27 PM9/11/06
to
fabio.b...@REMOVETHISTOMAILMEomega64.com wrote:
> I went across what seems possibly a bug of the compiler (VisualC 2005,
> just for the record) or a very strange and non-expected (by me at least)
> behaviour of the C++ ISO standard. Thus I'm posting on both newsgroups.
>
> Here we go: I noticed that if I nest a structure inside another one,
> the wrong constructor will be called.. the one from a different class!!

[code]

FWIW, GCC 4.1 disagrees with MSC14 in that aspect, it produces the same
output for both cases as one would expect.

Uli

Noah Roberts

unread,
Sep 11, 2006, 12:33:49 PM9/11/06
to
I get this:

playground.obj : fatal error LNK1179: invalid or corrupt file:
duplicate COMDAT
'??0<unnamed-tag>@MyFinalStruct_this_produces_the_bug@@QAE@XZ'

This is also VC++ 2005. Standard edition. This is a copy paste of the
OP code with namespace std and some includes.

Yeah, obviously buggy. I did a google search for this error and came
up with a number of things, all unrelated to each other and none fit
this situation.

Fun stuff.

Alex Blekhman

unread,
Sep 11, 2006, 1:44:05 PM9/11/06
to
<fabio.b...@REMOVETHISTOMAILMEomega64.com> wrote in
message
news:4505884b$0$30237$4faf...@reader1.news.tin.it...


I get the same linking error as Noah Roberts. Actually,
anonymous structures are not allowed by the C++ Standard,
however MSVC++ permits them as MS extension. When I added
names to MyFinalStruct_this_produces_the_bug nested structs
everything compiled and ran properly:

struct MyFinalStruct_this_produces_the_bug {
struct tagA {
Two MyObject1;
} A;
struct tagB {
Zwei MyObject2;
} B;
struct tagC {
Dos MyObject3;
} C;
} ;


Alf P. Steinbach

unread,
Sep 11, 2006, 1:57:49 PM9/11/06
to
* Alex Blekhman:

> anonymous structures are not allowed by the C++ Standard,

Aren't they?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Steve Pope

unread,
Sep 11, 2006, 2:25:29 PM9/11/06
to
Alf P. Steinbach <al...@start.no> wrote:

>* Alex Blekhman:

>> anonymous structures are not allowed by the C++ Standard,

>Aren't they?

This would seem a good thing to clear up. ;) Seems you
need them for C back-compatibility.

Steve

Alex Blekhman

unread,
Sep 11, 2006, 2:45:12 PM9/11/06
to
"Alf P. Steinbach" wrote:
>> anonymous structures are not allowed by the C++ Standard,
>
> Aren't they?


Hm.. It appears that you're right. I confused OP example
with declaration of unnamed anonymous struct:

// compiles with MS extensions enabled, fails with /Za
switch
struct A
{
struct
{
int i;
};
};


Ron Natalie

unread,
Sep 12, 2006, 4:38:50 PM9/12/06
to
Alex Blekhman wrote:

> I get the same linking error as Noah Roberts. Actually,
> anonymous structures are not allowed by the C++ Standard,

There is no such thing as an anonymous struct (in the sense
of an anonymous union) in C++. These are not anonymous structs
in that sense, they are just unnamed ones. Since the struct
does define a variable, it is not anonymous and legal in this
use.

Tim Roberts

unread,
Sep 13, 2006, 2:25:26 AM9/13/06
to
"Noah Roberts" <robert...@gmail.com> wrote:

>I get this:
>
>playground.obj : fatal error LNK1179: invalid or corrupt file:
>duplicate COMDAT
>'??0<unnamed-tag>@MyFinalStruct_this_produces_the_bug@@QAE@XZ'
>
>This is also VC++ 2005. Standard edition. This is a copy paste of the
>OP code with namespace std and some includes.
>
>Yeah, obviously buggy.

I also get the linker error, with both VC++ 2003 and VC++ 2005, but if you
look at the generated code with /Fc, you can see that it generates three
separate constructors, but gives them all the exact same name
(??0<unnamed-tag>@MyFinalStruct_this_produces_the_bug@@QAE@X). The
MyFinalStruct_this_produces_the_bug constructor then calls this name three
times.
--
- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

0 new messages