Affichage des articles dont le libellé est illegal. Afficher tous les articles
Affichage des articles dont le libellé est illegal. Afficher tous les articles

mercredi 29 janvier 2014

illegal void argument topic




On 29.01.2014 16:04, Öö Tiib wrote:
> In C++ we can have void return type and we can "return" it:
>
> void foo();
>
> void bar()
> {
> return (void)42; // ok #1
> return foo(); // ok #2
> }
>
> Fun way to confuse novices indeed. However, it appears that we can not (for
> whatever unknown reason) pass void arguments:
>
> void bad()
> {
> foo((void)42); // illegal
> }
>
> Even if I think I will be extra clever and add overload of 'foo' that supposedly
> accepts anything ...
>
> void foo(...);
>
> ... then I get different failures or successes on different mac/ubuntu clang/gcc
> versions. Seems that compilers are confused.
>
> Is there reason why we have such inconsistency?
>


`(void)` as a formal argument list doesn't indicate a formal argument of
type `void`. It's just a special syntax from C, where it specifies that
the function really doesn't take *any* arguments, at all.

In C++ the special syntax is unnecessary, since in C++ `()` also says
that, but the special syntax is supported for C compatibility.

Also baffling: why you can create a "void value" via `void()`. I guess
for uniform treatment in template code. But still it's kind of
inconsistent with the basic idea of `void` as an incomplete type.


Cheers & hth.,

- Alf