Re: 'void' : bug or feature?



Oh yeah, one more thing:

>>In your example the declaration introduces the global
>>void X_To_2() function.

Are you sure about this? Note that all definitions and even usages of
X_To_2() are entirely contained inside the scope of ClassA's definition.
Further, one 'version' of MethodB is a *private* method of ClassA, the other
a 'local method' to the public ClassA constructor (e.g., if I create a
'local variable' in a constructor it is not global). Why then would either
version be global?

I assume the linker doesn't complain about methods and functions declared
and not defined, IF they are never used. Otherwise I have even more
questions... ; )

[==P==]

"Holger Grund" <holger.grund@xxxxxxxxxxxxxxx> wrote in message
news:OLbAf4RHGHA.1676@xxxxxxxxxxxxxxxxxxxxxxx
> "Peter Oliphant" <poliphant@xxxxxxxxxxxxxxxx> wrote
>
>>I just found out the following code compiles:
>>
>> ref class ClassA
>> {
>> public:
>> ClassA()
>> {
>> x = 0 ;
>> X_To_1( ) ;
>> void X_To_2( ) ; // ****
>> Console::WriteLine( "x = {0}", x ) ; // "x = 1"
>> }
>>
> [..]
>> Note the line indicated by the asterisks above begins with a 'void'. What
>> this does is cause this line of code to be skipped. Hence, after the
>> constructor runs, x = 1 and not x = 2. Is this a bug or a feature?
>>
> The indicated line is simply a function declaration. Function scope
> function declarations introduce functions from the nearest containing
> namespace scope into the lexical scope. In your example the
> declaration introduces the global
> void X_To_2() function.
>
> You could write
> void X_To_2(); // declares ::X_To_2();
> X_To_2(); // calls ::X_To_2();
> ...
> }; // end of ref class ClassA
>
> void X_To_2(){ //..}
>
> Your member function declaration is in a different non-overlapping
> scope. They are completely unrelated.
>
>> The way I found this is I copy-and-pasted the function declaration to the
>> code and didn't remove the 'void' from in front. So it's an easy trap to
>> fall into...
>>
> There's another common pitfall. Function declarations vs local variable
> with initializers:
>
> struct Y{}; struct X{ X(const Y&);};
> void foo() {
> // Want to construct an X with a default constructed Y into variable x
> X x( Y() );
> // however, this declares a function x which returns an X and takes a
> // parameter of type Y() which is adjusted to Y(*)(), i.e.
> // a function pointer taking no arguments and returning a Y
>
> -hg
>
>
>


.



Relevant Pages

  • Re: void : bug or feature?
    ... > void MethodA() ... >>> ref class ClassA ... >> The indicated line is simply a function declaration. ... >> namespace scope into the lexical scope. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: void : bug or feature?
    ... >> ref class ClassA ... >> Note the line indicated by the asterisks above begins with a 'void'. ... > The indicated line is simply a function declaration. ... > namespace scope into the lexical scope. ...
    (microsoft.public.dotnet.languages.vc)
  • [PATCH] update checkpatch.pl to version 0.17
    ... This version brings improvements to external declaration detection, ... to quote tracking, fixes to unary tracking, some clarification of wording, ... detect external function declarations without an extern prefix ... function declaration arguments should be with the identifier ...
    (Linux-Kernel)
  • Re: Temporary objects as constructor arguments?
    ... >> the attempt to call a method fails for these. ... The fifth is a declaration. ... declaration of a function which returns a ROD, ... would have expected this to fail to match the function declaration. ...
    (comp.lang.cpp)
  • Confusion in ANSI Cs function concepts
    ... is no function declaration corresponding to the function call and the ... call does not say anything about the return type then the compiler ... in the above case the return type is mentioned as void *. ...
    (comp.lang.c)

Quantcast