Groups | Blog | Home
all groups > visual c > january 2004 >

visual c : hm, is this ok to compile without any error? (vc71)


Gabest
1/7/2004 12:09:24 AM
class X
{
public:
X() {_tprintf(_T("Hey!\n"));}
};

int _tmain(int argc, _TCHAR* argv[])
{
X(qwerty);
return 0;
}

output: Hey!

David Lowndes
1/7/2004 9:27:43 AM
Yes, it compiles OK with VC7.1, and it's also ok with the online
Comeau compiler.

What were you trying to point out?

Dave
--
Gabest
1/7/2004 8:59:21 PM
[quoted text, click to view]

Just strange that class X takes no parameter in its constructor and I can
still pass one: "qwerty", which was defined nowhere.

Gabest
1/7/2004 9:10:14 PM
One more note about why I asked if it was ok. This behavior from the
compiler confused me yesterday when I accidentally renamed one of my
functions to an already existing class name. Then it didn't compile of
course. But when I was renaming the function and the refereces back what
they were before, I forgot to do it at one place, and that called the
default constructor of the class instead of my old function. It took me a
few minutes to realize what was happening :)

Hendrik Schober
1/7/2004 9:13:22 PM
[quoted text, click to view]


I think it's just taking this
X(qwerty);
part as a function declaration.
That's a rather common annoyance
in C++.

Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

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

Gabest
1/7/2004 9:32:41 PM
[quoted text, click to view]

You mean function X with the default int return type? What would be the type
of qwerty then, also int? Why does my sample code still print out "Hey!"?

Hendrik Schober
1/7/2004 10:41:36 PM
[quoted text, click to view]


You're right.
I just played with this and it is
indeed calling the default ctor
three times:

class X
{
public:
X()
{
}
};

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

(Note: I removed all headers to make
sure there's no weird macro involved.)

And, yes, Comeau (4.3.3) accepts it as well:

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

C:\Develop>test.exe
Ooops!
Ooops!
Ooops!

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

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

int main(int /*argc*/, char* /*argv*/[])
{
X(blah);
X();
X x;
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!
Ooops!
Ooops!

C:\Develop>

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

Puzzled,

Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

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

David Lowndes
1/8/2004 12:32:42 AM
[quoted text, click to view]

Haven't you actually just got an instance of X named qwerty?

X qwerty;

Dave
--
Bo Persson
1/8/2004 12:40:10 AM

"Hendrik Schober" <SpamTrap@gmx.de> skrev i meddelandet
news:%23lJ3wcW1DHA.3824@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

But....

This is yet another case of the redundant parenthesis in a declaration!

X(blah);

is equivalent to

X blah;

which is a declaration of a local variable blah of type X.


Bo Persson


Hendrik Schober
1/8/2004 11:45:45 AM
[quoted text, click to view]


Thanks. I didn't see it...

Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

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

Jigar Mehta
1/8/2004 6:25:08 PM
Great!!
Thanks for the information I have got from this thread.. It solved one
unsolved problem in my project...

Saved me a lot of time...

Jigar Mehta
jbmehtain@yahoo.co.in


Gabest
1/8/2004 7:08:01 PM

[quoted text, click to view]

Can you see any in the posted sample? :) That one compiles as is, only needs
a standard c header for printf.

Gabest
1/8/2004 7:13:26 PM
[quoted text, click to view]

Oh, and is this legal or a bug? Can I also write (X) blah, X()blah, ()X
blah, (X blah) then, just for fun? :P

David Lowndes
1/8/2004 8:54:58 PM
[quoted text, click to view]

X(qwerty);

is effectively:

X qwerty;

?

Dave
--
Bo Persson
1/9/2004 12:13:20 AM

"Gabest" <gabest@freemail.hu> skrev i meddelandet
news:ehkYaMh1DHA.3220@tk2msftngp13.phx.gbl...
[quoted text, click to view]

No, this is legal (but *very* confusing).

[quoted text, click to view]

No, its no fun at all. :-)


The "thing" being declared can be put inside a pair of parenthesis, because
sometimes it is needed ...

To declare a pointer to a function, you actually need to write

return_type (*Fptr)(param_type);

to show that the star goes with Fptr and not with the return type. If you
write it like

return_type * FPtr(param_type)

it's suddenly a function returning a pointer, not a pointer to a function
....

So, to make it "simple" you are just always allowed to put the name you
declare inside a pair of parenthesis. I guess it was just to difficult to
specify exactly when it could be needed!

Now we can write our declarations above as:

return_type (*Fptr)(param_type); // pointer to function returning a value
and
return_type* (Fptr)(param_type); // function returning a pointer to a
value


Unfortunately this rule makes this ok, and meaning the same:

type variable;
type (variable);
type(variable);

but the last one looks terribly much like these

class_name(initial_value);
function_name(formal_parameter);


The language rules simply say that if you can't tell, it is to be seen as a
declaration.


Bo Persson

Bo Persson
1/9/2004 12:21:49 AM

"Bo Persson" <bop@gmb.dk> skrev i meddelandet
news:O3QQE0j1DHA.2388@TK2MSFTNGP09.phx.gbl...

[quoted text, click to view]

No, that wasn't strong enough!

If it can, by any stretch of the imagination, be seen as a declaration, it
must be considered a declaration. Even if it then doesn't compile!




Bo Persson



AddThis Social Bookmark Button