void and this

  • Thread starter Jonathan Mcdougall
  • Start date
J

Jonathan Mcdougall

If memory serves, in the C language, foo(void) means no
I understand the distinction and the history, but I still fail to
understand why (void) is frowned upon or viewed as "C".
I happen to like the explicit way in which it says "this function
takes no arguments".

int f();

could not be more explicit. What's more, I think (void) is
not only bad style, but deprecated. But I could be wrong.


Jonathan
 
B

Bob Bell

tom_usenet said:
Jonathan Mcdougall said:
I have a class member function declared as

class some_class {
...
virtual int call(void);

void in an empty parameter list is considered bad style in C++.
[...]

Since when?

Dennis Ritchie and Doug Ilroy described use of void to represent an
empty parameter list in C as an "abomination". Stroustrup agrees with
them, and void f(void); was illegal syntax in the early days of C++.
However it was reincorporated into C++ when C89 decided to use that
syntax in the interests of compatibility.

It's a good thing it was reincorporated, then. If I remember
correctly, there are contexts where foo() can be (mis)interpreted as a
call to foo() rather than a declaration of it; that never occurs with
foo(void).

(It's a subtle issue with templates, but I'll have to post an example
when I get home, as I don't have my copy of C++ Templates: The
Complete Guide. On the other hand, I might be remembering it wrong.)

Bob
 
N

Noah Roberts

#define THIS_FUNCTION_NIETHER_NEED_NOR_ACCEPTS_PARAMETERS

int f(THIS_FUNCTION_NIETHER_NEED_NOR_ACCEPTS_PARAMETERS);
void g(int x);
void q(THIS_FUNCTION_NIETHER_NEED_NOR_ACCEPTS_PARAMETERS);

All code that doesn't use the above is sux.

NR
 
N

Noah Roberts

Vladimir said:
Hello,

I have a class member function declared as

class some_class {
...
virtual int call(void);
};

Can I use this-> inside the function body?

You can, and in fact you may run into someone that requires it in their
coding standards. I personally have not, but some people prefer that
you explicitly cass an instance variable like this->xxx instead of just xxx.

NR
 
K

Kevin Goodsell

David said:
I understand the distinction and the history, but I still fail to
understand why (void) is frowned upon or viewed as "C".
I happen to like the explicit way in which it says "this function
takes no arguments".

Well, I don't mean to be argumentative, but it seems to me that showing
the function with no arguments is possibly a *more* explicit (or at
least logical) way of showing that it takes no arguments than showing it
with what appears to be a void argument. Granted, no C++ programmer will
actually be lead to believe that the function takes one argument of
type void, but that is what the syntax suggests. This is why I agree
that the syntax is an "abomination", as was stated elsewhere.

On the other hand, (void) is usually the right thing to use in C - if
you were concerned about forming a habit that will cause problems if
carried over to C, I can sort of understand that. I could also
appreciate wanting to avoid confusing newbies into carrying the use of
empty parens over to C.

-Kevin
 
D

David B. Held

Jonathan Mcdougall said:
[...]
int f();

could not be more explicit.

Sure it could. Add "void". Then you aren't wondering if you
are looking at a C++ function that takes no arguments, or a
C function that takes an unknown number of arguments.
Because we all know that if you don't do that, Russia will
invade, we will be attacked by aliens, and global warming
will cause the earth to burn up. That's what will happen if you
don't use "void".
What's more, I think (void) is not only bad style, but
deprecated.

LOL!!! As if (void) is so evil that we must get rid of it at all
costs!
But I could be wrong.

I certainly hope so. There's much worse things to worry about
than (void).

Dave
 
D

David B. Held

Kevin Goodsell said:
Well, I don't mean to be argumentative,

Don't apologize for disagreeing!
but it seems to me that showing the function with no
arguments is possibly a *more* explicit (or at least logical)
way of showing that it takes no arguments than showing it
with what appears to be a void argument. Granted, no C++
programmer will actually be lead to believe that the function
takes one argument of type void, but that is what the syntax
suggests. This is why I agree that the syntax is an
"abomination", as was stated elsewhere.
[...]

Well, void doesn't quite work with metaprogramming, but it
should. I mean, how handy would it be if we could substitute
any number of void arguments in a template function? We
wouldn't need to write a hundred overloads! Like this:

int foo(void, void); // takes no arguments

Why would you want to do that? Why, so you could do this,
of course:

template <typename T1 = void, typename T2 = void, ...>
int foo(T1 t1, T2 t2, ...);

And voila! You just wrote N overloads that currently require
tedious cut-n-paste or preprocessor magic.

Anyway, I think of "void" as a "metanull". It's a metavalue
which indicates nothingness, like NULL, kinda. Would you
say that this is more "explicit"?

int* p = ;

Believe it or not, I see "int foo()" in a similar way, which
rubs me the wrong way just as the statement above
undoubtedly rubs most people.

Dave
 
A

Attila Feher

David said:
WW said:
Stop trolling please.

[Earlier, you said...]
And as everything redundant adding no value, the only
thing it can add is error, misunderstanding, misleading,
wondering collegagues why is it there and so forth.

Can you spell "FUD"? Physician, heal thyself.

Stop trolling please.
 
N

Noah Roberts

Attila said:
David said:
jeffc wrote:


So why do you have to pick a quarrel on everything?

bwahahahaahahaha

Stop trolling please.

[Earlier, you said...]
And as everything redundant adding no value, the only
thing it can add is error, misunderstanding, misleading,
wondering collegagues why is it there and so forth.

Can you spell "FUD"? Physician, heal thyself.


Stop trolling please.

He's like a broken record :p
 
A

Attila Feher

Noah said:
So why do you have to pick a quarrel on everything?

bwahahahaahahaha

Stop trolling please.

[Earlier, you said...]

And as everything redundant adding no value, the only
thing it can add is error, misunderstanding, misleading,
wondering collegagues why is it there and so forth.

Can you spell "FUD"? Physician, heal thyself.


Stop trolling please.

He's like a broken record :p

Yes, he is. I wish he would stop trolling.
 
R

Rolf Magnus

David said:
Jonathan Mcdougall said:
[...]
int f();

could not be more explicit.

Sure it could. Add "void".

How does adding a pseudo parameter of type void make it more expicit
that you want no parameters?
Then you aren't wondering if you
are looking at a C++ function that takes no arguments, or a
C function that takes an unknown number of arguments.

I never was wondering about that. I typically know if I'm programming in
C or in C++. Don't you too?
Because we all know that if you don't do that, Russia will
invade, we will be attacked by aliens, and global warming
will cause the earth to burn up. That's what will happen if you
don't use "void".

.... in C. Well, actually only in the obsolete C89.
 
R

Rolf Magnus

David said:
Well, void doesn't quite work with metaprogramming, but it
should. I mean, how handy would it be if we could substitute
any number of void arguments in a template function? We
wouldn't need to write a hundred overloads! Like this:

int foo(void, void); // takes no arguments

Why would you want to do that? Why, so you could do this,
of course:

template <typename T1 = void, typename T2 = void, ...>
int foo(T1 t1, T2 t2, ...);

And voila! You just wrote N overloads that currently require
tedious cut-n-paste or preprocessor magic.

The question is where could you practically use it? I mean, you can't
use the parameters within your function, because they might not exist.
So what are they useful for then?
Anyway, I think of "void" as a "metanull". It's a metavalue
which indicates nothingness, like NULL, kinda. Would you
say that this is more "explicit"?

int* p = ;

Well, you also don't write:

void x;

to state that x is not a variable.
 
A

Attila Feher

Rolf said:
The question is where could you practically use it? I mean, you can't
use the parameters within your function, because they might not exist.
So what are they useful for then?


Well, you also don't write:

void x;

to state that x is not a variable.

We can take any number of void arguments, but we will not be convinced by
them. ;-)
 
J

Jonathan Mcdougall

int f();
Sure it could. Add "void".

At that point, I think it must only be a matter of taste.
Then you aren't wondering if you
are looking at a C++ function that takes no arguments, or a
C function that takes an unknown number of arguments.

I don't program in C, so there is no wondering about that.
LOL!!! As if (void) is so evil that we must get rid of it at all
costs!

No, only that it is useless. Not many things are useless in C++.
Take for example 'typename' which is only allowed in certain
circumstances since it would be useless (but not syntactically).
I certainly hope so. There's much worse things to worry about
than (void).

That I certainly agree with.


Jonathan
 
W

WW

David said:
To each his own. When I see (int) 3.14, I think "C". But that's
because there's a *better* way to do it in C++. When I see
"(void)", I *don't* think "C", because the "C++ Way" isn't
technically better. It's just different.

If we say "you are right" will you stop ranting?
 
D

David B. Held

WW said:
If we say "you are right" will you stop ranting?

No, because A) you asked a loaded question, and B) I don't
think the issue has an unarguably correct position.

Dave
 
D

David B. Held

Rolf Magnus said:
[...]
The question is where could you practically use it? I mean,
you can't use the parameters within your function, because
they might not exist. So what are they useful for then?

You use type traits to see if an argument is void or not.
That's what is_void<> is for. To be truly useful, these functions
would have to be considered equivalent for overload purposes:

int foo(int);
int foo(int, void);

Then, you could do c'tor forwarding very nicely:

template <typename T, typename A1, typename A2 = void, ...>
auto_ptr<T> make_auto_ptr(A1 a1, A2 a2, ...)
{
return new T(a1, a2, ...);
}

The call to new T() would only call c'tors that matched the right
number of non-void arguments. Of course, 'void' in the middle
of a non-void list would be ill-formed. Compare that to the
current way we have to do this with N overloads.

There are all kinds of places where forwarding occurs, or
doesn't occur, because it's too tedious to write the overloads.
[...]
void x;

to state that x is not a variable.

But you do here:

int foo(void);

You just aren't allowed to name the "not-present variable".
And you can certainly cast to void:

(void) x;

Dave
 
D

David B. Held

Attila Feher said:
[...]
And as everything redundant adding no value, the only
thing it can add is error, misunderstanding, misleading,
wondering collegagues why is it there and so forth.
[...]
Yes, he is. I wish he would stop trolling.

So you're saying your statement above about (void) has a
factual basis? Would you like to prove your statement then,
with replicated studies? Or are you just going to admit
that you made a provokative unfounded claim?

Dave
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top