473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

'void' : bug or feature?

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

~ClassA() {}

private:
void X_To_1() { x = 1 ; }
void X_To_2() { x = 2 ; }

private:
int x ;
} ;

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

[==P==]
Jan 19 '06 #1
7 1000
"Peter Oliphant" <po*******@RoundTripInc.com> 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

Jan 19 '06 #2
Peter Oliphant wrote:
void X_To_2( ) ; // ****

What this does is cause this line of code to be skipped.


It simply declares the function, and does nothing else.

Tom
Jan 19 '06 #3
Interesting. Why is it legal to declare function in the middle of a method?
What purpose does this have? Can I define a whole method inside the code for
another method? Something like:

void MethodA()
{
// code
void MethodB() { /* code */}
//code
MethodB() ; // call
// code
}

If this is possible, what is it used for? Why wouldn't it be better to
define MethodB totally external to MethodA (but still in the same class)? I
assume this would mean that ONLY MethodA has access to it ala scoping rules
(i.e., it is a 'local method' to MethodA)...

If it's not possible, why is declaring a method in the middle of another
method's code legal?

IMHO, this is a case where the benefit for adding functionality is
outweighed by the side-effect of the pitfalls it introduces...

[==P==]

"Holger Grund" <ho**********@remove.ix-n.net> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl...
"Peter Oliphant" <po*******@RoundTripInc.com> 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

Jan 19 '06 #4
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" <ho**********@remove.ix-n.net> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl... "Peter Oliphant" <po*******@RoundTripInc.com> 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

Jan 19 '06 #5

"Peter Oliphant" <po*******@RoundTripInc.com> skrev i meddelandet
news:Oc**************@TK2MSFTNGP12.phx.gbl...
Interesting. Why is it legal to declare function in the middle of a
method?
It is legal to put declarations in the middle of a function, like "int
i;".

Declaring a functions is just not ruled out.
What purpose does this have? Can I define a whole method inside the
code for another method?
Nested functions are not allowed. You could declare a class though
(with members functions).

[snip]

If this is possible, what is it used for?
Who says it's any good? :-)

It is just not forbidden.
Why wouldn't it be better to define MethodB totally external to
MethodA (but still in the same class)?
Of course.

If it's not possible, why is declaring a method in the middle of
another method's code legal?
Just because it is not illegal.
IMHO, this is a case where the benefit for adding functionality is
outweighed by the side-effect of the pitfalls it introduces...


Sure. Everyone agrees on this one.

If it hadn't been this way in C for decades, it would never have
appeared in C++.
Bo Persson

ref class ClassA
{
public:
ClassA()
{
x = 0 ;
X_To_1( ) ;
void X_To_2( ) ; // ****
Console::WriteLine( "x = {0}", x ) ; // "x = 1"
}

[..]

Jan 19 '06 #6
Peter Oliphant wrote:
Are you sure about this?
Yes, it is absolutely positively a function declaration.
Why then would either version be global?
If you declare a function outside of a class declaration, it is global.
It declares a global function "void X_To_2()", which is never implemented.
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... ; )


If you start calling it, the linker will complain, otherwise it won't.
There's no such thing as linker warning indicating dead function
declarations. The compiler can not possibly know if your function will
be used or not, because it doesn't see the whole picture, only one unit
at a time. The linker doesn't know it either, because it only knows
about functions actually being called.

Tom
Jan 19 '06 #7

"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...
Interesting. Why is it legal to declare function in the middle of a
method? What purpose does this have? Can I define a whole method inside
the code for another method? Something like:

void MethodA()
{
// code
void MethodB() { /* code */}
//code
MethodB() ; // call
// code
}

You can define almost anything inside of the function, but not a function.
You can use operator () and define functor instead, if you want. (This could
be quite handy sometimes, having the functor defined near where it is used -
unfortunatelly C++ standard currently does not allow template instantiation
with such local types).

void MethodA()
{
class MethodB
{
public:
void operator () () const
{
// code goes here
}
};
//code
MethodB()() ; // construct and call in one line
MethodB b();
b() ; // another way to call
}
If it's not possible, why is declaring a method in the middle of another
method's code legal?
Sometimes you may want some external function to be visible only in some
particular function, sometimes you might want to do this just because you
are lazy ;) and do not want to scroll outside of the long function, body. It
is not a common practice though, and I think it is not even a good practice,
because it can easily break modularity of your code by creating hidden
dependencies.

Regards
Ondrej

If this is possible, what is it used for? Why wouldn't it be better to
define MethodB totally external to MethodA (but still in the same class)?
I assume this would mean that ONLY MethodA has access to it ala scoping
rules (i.e., it is a 'local method' to MethodA)...
IMHO, this is a case where the benefit for adding functionality is
outweighed by the side-effect of the pitfalls it introduces...

[==P==]

"Holger Grund" <ho**********@remove.ix-n.net> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl...
"Peter Oliphant" <po*******@RoundTripInc.com> 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


Jan 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
3
by: bnoordhuis | last post by:
Consider this: int foo(int *a, int *b); int (*bar)(void *, void *) = (void *)foo; How legal - or illegal - is the typecast and are there real-world situations where such code will cause...
6
by: rahulsinner | last post by:
Hello everyone, I have noticed everyone using "int main(void)".But doesnt the standard pronounces it as "int main(int argc,char *argv)".And if i don't specify arguments,why can't i simply put...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
28
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a...
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
36
by: x_knifer_x | last post by:
read the subject it says it all int main(void) is better than int main()
10
by: Chris Becke | last post by:
In C and in older C++ compilers, you could write code like this void a(void** pp); // declare a function that takes a pointer to a pointer-to-void. struct I {}; // some struct. I *i1,**i2; //...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.