Why does this compile?

  • Thread starter Hendrik Schober
  • Start date
H

Hendrik Schober

--8<----8<----8<----8<----8<----8<----8<----8<--

C:\Develop>type test.cpp
#include <stdio.h>

class X
{
public:
X()
{
printf("Ooops!\n");
}
};

int main(int /*argc*/, char* /*argv*/[])
{
X(blah);
return 0;
}

C:\Develop>como --A -otest.exe test.cpp
Comeau C/C++ 4.3.3 (Aug 10 2003 15:39:53) for _MS_WINDOWS_x86_Beta8
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++


C:\Develop>test.exe
Ooops!

C:\Develop>

-->8---->8---->8---->8---->8---->8---->8---->8--

Puzzled,

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
R

Ron Natalie

Your compiler is defective. Possibly, something that stdio.h brings
in does something like:
#define blah

What happens if you do
#undef blah
just before main?
 
B

Bryan Bullard

no he's right. i just compiled this on both ms vc and gnu c on cygwin.

-bryan
 
H

Heinz Ozwirk

: --8<----8<----8<----8<----8<----8<----8<----8<--
:
: C:\Develop>type test.cpp
: #include <stdio.h>
:
: class X
: {
: public:
: X()
: {
: printf("Ooops!\n");
: }
: };
:
: int main(int /*argc*/, char* /*argv*/[])
: {
: X(blah);
: return 0;
: }
:
: C:\Develop>como --A -otest.exe test.cpp
: Comeau C/C++ 4.3.3 (Aug 10 2003 15:39:53) for _MS_WINDOWS_x86_Beta8
: Copyright 1988-2003 Comeau Computing. All rights reserved.
: MODE:strict errors C++
:
:
: C:\Develop>test.exe
: Ooops!
:
: C:\Develop>
:
: -->8---->8---->8---->8---->8---->8---->8---->8--
:
: Puzzled,

Puzzling, yes, but the compiler is right. 'X(blah)' declares a variable named 'blah' of type 'X' and calls X's default constructor. C** (and C) allow ( and ) in many, sometimes unexpected places, including declarations. Usually they are used in more complex declarations like

int (*x)[42]; // pointer to array
int *(a[42]); // array of pointers

But something like

int (x); // just an int

is perfectly legal.

HTH
Heinz
 
R

Ron Natalie

Bryan Bullard said:
no he's right. i just compiled this on both ms vc and gnu c on cygwin.
Yep, it defines a variable of type X called blah. The "parens get ignored"
in declarations rule strikes again!
 
R

Rob Williscroft

Hendrik Schober wrote in
--8<----8<----8<----8<----8<----8<----8<----8<--

C:\Develop>type test.cpp
#include <stdio.h>

class X
{
public:
X()
{
printf("Ooops!\n");
}
};

int main(int /*argc*/, char* /*argv*/[])
{

Duh, I had to compile this for some reason before I spotted the obvious

The above defines a variable named blah of type X, i.e.

X blah;
return 0;
}

Rob.
 
C

Cy Edmunds

Ron Natalie said:
Yep, it defines a variable of type X called blah. The "parens get ignored"
in declarations rule strikes again!

C++: love the language, hate the syntax. Ugh.
 
H

Hendrik Schober

Hendrik Schober said:


Thanks! I suspected the decl/def-thing
to be responsible for this, but I didn't
see a variable definition in this.

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 

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,770
Messages
2,569,585
Members
45,081
Latest member
AnyaMerry

Latest Threads

Top