Ben Bacarisse said:
Nick Keighley said:
Navaneeth <
[email protected]> writes:
[...] code which works in both C and C++ compilers.
and, most of the time, it is for really trivial crap, like, casting the
result of malloc/realloc calls, or using "()" for no-argument functions
rather than "(void)", ...
just curious, what's wrong with using (void) for an empty argment list
in C++? I always thought this was just a style thing
Yes, I am sure it's fine (bar style issues). The problem comes when one
goes the other way: using idiomatic C++ (e.g. void f()
in C because
you then loose the checking on the function call. I presume that BGB
uses the less idiomatic C++ version (void f(void)
in order to get the
benefit of switching between compilers, but the text suggests otherwise.
I had thought "(void)" was disallowed in C++, hmm...
well, anyways, I normally use "()" in either case, as personally I find it
looks nicer, and also looks more like most other languages (like JS, C#, and
Java, which I also use sometimes), and also like the notation used when
calling said function (arguments checking or not...).
one could argue though: how often is it that anyone will actually try to
pass arguments to a no-argument function, or more so, what would it sensibly
do with said arguments anyways, besides ignore them?...
admittedly though, it is fairly rare that I don't pass arguments to
functions, since the usual role of a function is to accept inputs and
produce outputs, it generally is fairly rare that one will have a function
with only outputs (or, a no-argument, no-return function, that is much
different from being a no-op...).
granted, this may be that, as a general rule, I prefer to avoid using
globals either (almost everything is either passed as arguments, or stored
in a "context" which is usually also passed as an argument, meaning that
no-argument functions are fairly rare in practice...).
partial reason for not liking using globals:
they mix poorly with multi-threading, as then often one needs mutexes or
similar to keep threads from stepping on each other. it is typically cleaner
IMO to use context structs, and assume that threads will not share these
structs (or, at least, if the contents of the struct are stateful, where it
is much safer to share stateless/immutable data).
or such...