Topic: Do you use () or (void)?
Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 4 Nov 1994 15:38:13 GMT Raw View
In article <38u2fo$bs8@engnews2.Eng.Sun.COM>, clamage@Eng.Sun.COM (Steve Clamage) writes:
|> wilkinso@gdls.com (Robert M. Wilkinson) writes:
[..]
|> Everyone who has ever had to parse C++ code would prefer to drop the
|> "default int" rule entirely and require a type specification.
Yes, drop this "default int" rule!
Pro:
1. This would be one chance to simplify a little bit the description and
learning of C++ as it is no longer necessary to mention that "int" is the
default returntype and explain why it is "int" and not "void".
2. C++ compilers can be simplified by not implementing this rule.
3. C++ code that uses this rule is confusing to read.
Non-con:
4. I can't remember C++ code that uses this rule. [IMHO it's bad style]
Con:
5. Some C-headers may get broke [like they did by introducing: () == (void)]
|> Defaulting to void doesn't make (IMHO) any more sense than defaulting to int.
Absolutly.
[..]
|> --
|> Steve Clamage, stephen.clamage@eng.sun.com
Ulf Schuenemann
--------------------------------------------------------------------
Ulf Sch nemann
Institut f r Informatik, Technische Universit t M nchen.
email: schuenem@informatik.tu-muenchen.de
Author: adam@comptech.demon.co.uk (Adam Goodfellow)
Date: Thu, 20 Oct 1994 12:31:13 GMT Raw View
In article <37v6p3$opo@net.auckland.ac.nz>,
Edwin Ng <edwin@ccu1.auckland.ac.nz> wrote:
>
>Hi. I'm a great fan for type-checking in C++ and I've been
>drawn into a debate as to whether I should use () or (void)
>when declaring constructors/destructors?
>
>I believe that they mean the same thing but I prefer (void).
>My colleage claims to have problems in my classes which use
>void in the default constructors and destructors.
>
>All the examples I've seen in OWL, MFC and g++ use ().
>Can someone clarify this for me? Thanks a lot.
>
(void) is ANSI C for no parameters, () is C++ for no paramters.
() in C is old K&R style C function declarations.
I think this use void was introduced into ANSI C to differentiate K&R
and ANSI syntax so a compiler knew what is was dealing with.
The only time you should use f(void) in C++ rather than f() is in the
case of header files that need to be acceptable to both ANSI C and C++.
--
Adam
======================================================================
| Computech Tel/Fax: 081 673 7817 email: adam@comptech.demon.co.uk |
======================================================================
Author: wilkinso@gdls.com (Robert M. Wilkinson)
Date: 27 Oct 1994 15:09:43 -0400 Raw View
In article <387r09$dd0@uqcspe.cs.uq.oz.au>,
>
Warwick Allison <warwick@cs.uq.oz.au> wrote:
>adam@comptech.demon.co.uk (Adam Goodfellow) writes:
>
>>(void) is ANSI C for no parameters, () is C++ for no paramters.
>>() in C is old K&R style C function declarations.
>
>(void) was introduced into ANSI C so that () could mean "any number
>of parameters" as it always meant in K&R C (ie. for backward compat.)
>
>C++ was being designed before the ANSI C definition was finished
>and was also not so compelled to be K&R compatible, so it (fortunately)
>uses ().
I've always wondered why C++ didn't default unspecified return types to
void. It seems more natural that if it isn't specified, then it doesn't
return anything. Of course, there's the C compatibility issue, but if
they're breaking compatibility with f(), why not also break it with the
return type?
--
____________________________________________________________________________
Rob Wilkinson wilkinso@gdls.com
____________________________________________________________________________
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 29 Oct 1994 17:58:16 GMT Raw View
wilkinso@gdls.com (Robert M. Wilkinson) writes:
>I've always wondered why C++ didn't default unspecified return types to
>void. It seems more natural that if it isn't specified, then it doesn't
>return anything. Of course, there's the C compatibility issue, but if
>they're breaking compatibility with f(), why not also break it with the
>return type?
Different category of "breaking compatibility".
In the interest of type safety, C++ requires functions to have prototypes.
A default return type is not a type safety issue; if you leave off a
specification, it is equivalent to having specified 'int'.
Everyone who has ever had to parse C++ code would prefer to drop the
"default int" rule entirely and require a type specification. Defaulting
to void doesn't make (IMHO) any more sense than defaulting to int. It
would introduce a gratuitous incompatibility, since it confers no
advantage.
--
Steve Clamage, stephen.clamage@eng.sun.com
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sat, 22 Oct 1994 09:04:41 GMT Raw View
In article <19941020.123113.39@comptech.demon.co.uk> adam@comptech.demon.co.uk writes:
>In article <37v6p3$opo@net.auckland.ac.nz>,
> Edwin Ng <edwin@ccu1.auckland.ac.nz> wrote:
>
>>
>>Hi. I'm a great fan for type-checking in C++ and I've been
>>drawn into a debate as to whether I should use () or (void)
>>when declaring constructors/destructors?
>>
>>I believe that they mean the same thing but I prefer (void).
>>My colleage claims to have problems in my classes which use
>>void in the default constructors and destructors.
>>
>>All the examples I've seen in OWL, MFC and g++ use ().
>>Can someone clarify this for me? Thanks a lot.
>>
>
>(void) is ANSI C for no parameters, () is C++ for no paramters.
>() in C is old K&R style C function declarations.
>
>I think this use void was introduced into ANSI C to differentiate K&R
>and ANSI syntax so a compiler knew what is was dealing with.
This is misinformation. In ANSI C, an empty formals list means that
the function can be called with any number of actual arguments, whereas
a list consisting of just `void' means that the function can only be
called with exactly zero actual arguments.
>The only time you should use f(void) in C++ rather than f() is in the
>case of header files that need to be acceptable to both ANSI C and C++.
This is also misinformation.
Don't trust everything you read here folks.
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -
Author: cperrott@retina.mqcs.mq.oz.au (Chris Perrott)
Date: 23 Oct 1994 06:03:39 GMT Raw View
In article <rfgCy2H7u.Czr@netcom.com> rfg@netcom.com (Ronald F. Guilmette) writes:
>In article <19941020.123113.39@comptech.demon.co.uk> adam@comptech.demon.co.uk writes:
>>In article <37v6p3$opo@net.auckland.ac.nz>,
>> Edwin Ng <edwin@ccu1.auckland.ac.nz> wrote:
>>
>>>
>>>Hi. I'm a great fan for type-checking in C++ and I've been
>>>drawn into a debate as to whether I should use () or (void)
>>>when declaring constructors/destructors?
>>>
>>>I believe that they mean the same thing but I prefer (void).
>>>My colleage claims to have problems in my classes which use
>>>void in the default constructors and destructors.
>>>
>>>All the examples I've seen in OWL, MFC and g++ use ().
>>>Can someone clarify this for me? Thanks a lot.
>>>
>>
>>(void) is ANSI C for no parameters, () is C++ for no paramters.
>>() in C is old K&R style C function declarations.
>>
>>I think this use void was introduced into ANSI C to differentiate K&R
>>and ANSI syntax so a compiler knew what is was dealing with.
>
>This is misinformation. In ANSI C, an empty formals list means that
>the function can be called with any number of actual arguments, whereas
>a list consisting of just `void' means that the function can only be
>called with exactly zero actual arguments.
>
>>The only time you should use f(void) in C++ rather than f() is in the
>>case of header files that need to be acceptable to both ANSI C and C++.
>
>This is also misinformation.
>
WHY are these two points misinformation? They seem fairly reasonable to me,
although avoiding (void) in C++ seems to be just a matter of style.
>Don't trust everything you read here folks.
>
I certainly don't!
--
Chris Perrott
Author: mishad@iplbath.demon.co.uk (Misha Dorman)
Date: Mon, 24 Oct 1994 10:12:08 +0000 Raw View
In article <38cubr$dkt@sunb.ocs.mq.edu.au> cperrott@retina.mqcs.mq.oz.au writes:
>>>I think this use void was introduced into ANSI C to differentiate K&R
>>>and ANSI syntax so a compiler knew what is was dealing with.
>>
>>This is misinformation. In ANSI C, an empty formals list means that
>>the function can be called with any number of actual arguments, whereas
>>a list consisting of just `void' means that the function can only be
>>called with exactly zero actual arguments.
>>
>>>The only time you should use f(void) in C++ rather than f() is in the
>>>case of header files that need to be acceptable to both ANSI C and C++.
>>
>>This is also misinformation.
>>
>WHY are these two points misinformation? They seem fairly reasonable to me,
>although avoiding (void) in C++ seems to be just a matter of style.
>
>>Don't trust everything you read here folks.
>>
>I certainly don't!
>
>--
>Chris Perrott
>
As far as I am aware, the standard (ARM and WP) say that () and (void) are
equivalent. However, I have come across compilers that do not allow dtors
to be declared using the (void) syntax. This is presumably a compiler bug -
but we have to use real compilers, so it is something to be wary of.
Of course I can't remember which compiler it was....!
Misha Dorman
not seaking for....
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 24 Oct 1994 16:21:24 GMT Raw View
In article dkt@sunb.ocs.mq.edu.au, cperrott@retina.mqcs.mq.oz.au (Chris Perrott) writes:
>>>
>>>(void) is ANSI C for no parameters, () is C++ for no paramters.
>>>() in C is old K&R style C function declarations.
>>>
>>>I think this use void was introduced into ANSI C to differentiate K&R
>>>and ANSI syntax so a compiler knew what is was dealing with.
>>
>>This is misinformation. In ANSI C, an empty formals list means that
>>the function can be called with any number of actual arguments, whereas
>>a list consisting of just `void' means that the function can only be
>>called with exactly zero actual arguments.
>>
>>>The only time you should use f(void) in C++ rather than f() is in the
>>>case of header files that need to be acceptable to both ANSI C and C++.
>>
>>This is also misinformation.
>>
>WHY are these two points misinformation? They seem fairly reasonable to me,
>although avoiding (void) in C++ seems to be just a matter of style.
"Misinformation" means "information which is not correct", and that
is the case here. The poster complaining of misinformation also didn't
get it quite right, unfortunately.
In Standard C, the declaration "f()" means that no information is provided
about the number or types of parameters expected by function f. It does
not mean that f may be called with any number of arguments. If f is
called with a different number of arguments or incompatible argument
types compared to its definition, the results are undefined. If you
want to call a function with differing numbers of arguments you must use
the ellipsis notation, such as "printf(const char*, ...)", in both
the declaration and definition (still talking about programs conforming
to the requirements of Standard C).
In C++, it is overstating the case to say that you should or should not
use "f(void)" instead of "f()". It is purely a matter of style, and
makes no difference in the meaning of the program. As with nearly all
matters of style, consistency is probably more important than which
convention you pick.
As noted, if the same header file is to be used by both C++ and C compilers,
you would want to use "f(void)" to get the benefit of type checking in C.
Note, however, that function f would also have to be declared 'extern "C"'
to be usable from C. Finally, class member functions cannot be declared
'extern "C"', so for them the question reverts to one of style.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: edwin@ccu1.auckland.ac.nz (Edwin Ng)
Date: 18 Oct 1994 01:01:23 GMT Raw View
Hi. I'm a great fan for type-checking in C++ and I've been
drawn into a debate as to whether I should use () or (void)
when declaring constructors/destructors?
I believe that they mean the same thing but I prefer (void).
My colleage claims to have problems in my classes which use
void in the default constructors and destructors.
All the examples I've seen in OWL, MFC and g++ use ().
Can someone clarify this for me? Thanks a lot.
Edwin
--
-----------------------------------------------------
* Edwin Ng (edwin@ccu1.auckland.ac.nz) *
* E&E Engineering, University of Auckland *
* Private Bag 92019, Auckland, NEW ZEALAND *
* *
* Phone 64 9 373 7599 Ext 8104 Fax: 64 9 373 7461 *
-----------------------------------------------------
Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Tue, 18 Oct 1994 14:18:08 GMT Raw View
In article <37v6p3$opo@net.auckland.ac.nz>,
Edwin Ng <edwin@ccu1.auckland.ac.nz> wrote:
>
>Hi. I'm a great fan for type-checking in C++ and I've been
>drawn into a debate as to whether I should use () or (void)
>when declaring constructors/destructors?
>
>I believe that they mean the same thing but I prefer (void).
>My colleage claims to have problems in my classes which use
>void in the default constructors and destructors.
>
>All the examples I've seen in OWL, MFC and g++ use ().
>Can someone clarify this for me? Thanks a lot.
>
They mean the same thing. () is shorter than (void).
-- Pete
Author: pjl@graceland.att.com (Paul J. Lucas)
Date: Tue, 18 Oct 1994 03:58:49 GMT Raw View
In <37v6p3$opo@net.auckland.ac.nz> edwin@ccu1.auckland.ac.nz (Edwin Ng) writes:
>Hi. I'm a great fan for type-checking in C++ and I've been
>drawn into a debate as to whether I should use () or (void)
>when declaring constructors/destructors?
>I believe that they mean the same thing but I prefer (void).
>My colleage claims to have problems in my classes which use
>void in the default constructors and destructors.
>All the examples I've seen in OWL, MFC and g++ use ().
>Can someone clarify this for me? Thanks a lot.
1. WRONG NEWSGROUP! THis groups is for the C++ STanarD.
Get it?!
2. (void) is an abomination.
--
- Paul J. Lucas
AT&T Bell Laboratories
Naperville, IL
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 19 Oct 1994 17:25:59 GMT Raw View
In article opo@net.auckland.ac.nz, edwin@ccu1.auckland.ac.nz (Edwin Ng) writes:
>
>Hi. I'm a great fan for type-checking in C++ and I've been
>drawn into a debate as to whether I should use () or (void)
>when declaring constructors/destructors?
>
>I believe that they mean the same thing but I prefer (void).
>My colleage claims to have problems in my classes which use
>void in the default constructors and destructors.
In C++ the two forms are exactly equivalent, and you can use whichever
you like. (In C, of course, they mean different things.) If you get
different results for the two forms with a C++ compiler, there is
something wrong with the compiler.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: meli@js-sfbsun.cs.uni-sb.de (Meli Henry)
Date: 20 Oct 1994 18:19:29 GMT Raw View
Edwin Ng (edwin@ccu1.auckland.ac.nz) wrote:
: Hi. I'm a great fan for type-checking in C++ and I've been
: drawn into a debate as to whether I should use () or (void)
: when declaring constructors/destructors?
: I believe that they mean the same thing but I prefer (void).
: My colleage claims to have problems in my classes which use
: void in the default constructors and destructors.
: All the examples I've seen in OWL, MFC and g++ use ().
: Can someone clarify this for me? Thanks a lot.
: Edwin
In C++ you don't need to specify the void inside the parenthesis,
since func_name() as declaration means exactly the same as funct_name(void ).
Henri.
Author: jason@cygnus.com (Jason Merrill)
Date: Tue, 18 Oct 1994 07:57:29 GMT Raw View
>>>>> Edwin Ng <edwin@ccu1.auckland.ac.nz> writes:
> Hi. I'm a great fan for type-checking in C++ and I've been
> drawn into a debate as to whether I should use () or (void)
> when declaring constructors/destructors?
> I believe that they mean the same thing but I prefer (void).
> My colleage claims to have problems in my classes which use
> void in the default constructors and destructors.
> All the examples I've seen in OWL, MFC and g++ use ().
> Can someone clarify this for me? Thanks a lot.
If you want to parse the same declarations in C and C++, use (void).
Otherwise, it's entirely stylistic. () is shorter to type.
Jason
Author: warwick@cs.uq.oz.au (Warwick Allison)
Date: 21 Oct 1994 07:35:37 GMT Raw View
adam@comptech.demon.co.uk (Adam Goodfellow) writes:
>(void) is ANSI C for no parameters, () is C++ for no paramters.
>() in C is old K&R style C function declarations.
(void) was introduced into ANSI C so that () could mean "any number
of parameters" as it always meant in K&R C (ie. for backward compat.)
C++ was being designed before the ANSI C definition was finished
and was also not so compelled to be K&R compatible, so it (fortunately)
uses ().
--
Warwick
--
_-_|\ warwick@cs.uq.oz.au /
/ * <-- Computer Science Department, / WIT SPACE TO LET
\_.-._/ University of Queensland, /
v Brisbane, Australia. /
Author: hans@borneo.gmd.de (Hans Wegener)
Date: 18 Oct 1994 10:40:56 GMT Raw View
In article <37v6p3$opo@net.auckland.ac.nz>, edwin@ccu1.auckland.ac.nz (Edwin
Ng) writes:
|> Hi. I'm a great fan for type-checking in C++ and I've been
|> drawn into a debate as to whether I should use () or (void)
|> when declaring constructors/destructors?
|>
|> I believe that they mean the same thing but I prefer (void).
|> My colleage claims to have problems in my classes which use
|> void in the default constructors and destructors.
As far as I know, the two make sense and mean the same. I do use (void), for
example and can intermix this with ()-declarations without having problems.
But perhaps your colleague simply doesn't like you?
------------------------------------------------------------------------------
Hans Wegener Phone: +49 2241 14 2840
GMD SET/SKS Fax: +49 2241 14 2889
Schloss Birlinghoven
D - 53754 St. Augustin
Germany Internet: hans.wegener@gmd.de
------------------------------------------------------------------------------