Why doesn't void take void value in C ++?

I am wondering why C ++ does not define void through:

typedef struct { } void;

those. What is the meaning of a type that cannot be created, even if this setting does not generate code?

If we use gcc -O3 -S, then both of these methods produce identical assembler:

int main() { return 0; }

and

template <class T> T f(T a) { }
typedef struct { } moo;
int main() { moo a; f(a); return 0; }

It makes sense. A struct { }just takes an empty value, simple enough for optimization. Actually the weird part is that they produce different code without -O3.

You cannot, however, pull this trick simply typedef void moobecause void cannot accept any value, not even an empty one. Does this distinction have any utility?

, Haskell, , , ML, void, , , void *.

+5
3

void C ++. , , void* . , , void* (- typedef s, ++), .

, void*, . , , , void, . , , .

, void , . (.. ), void.

+4

void , ?

:

struct x; // incomplete
typedef x moo;
+3

void ?

.

: moo FuncName(...) - . , , " " - ; return value;, value - . return moo();.

, -, ? , . .

++ - , . , . , .

void : " ". " ". :

moo FuncName(...) { return moo(); }

moo x = FuncName(...);

, , . , . x -.

:

void FuncName(...) {}

void x = FuncName(...);

- . :

void FuncName(...) {}

moo x = FuncName(...);

, : FuncName .

++ , C, - , , , , .

, void* , void . , , a void* " ". . . , .

moo*, . . ++ ( ) undefined. moo. , , :

moo *m = new moo;
*moo;

void . moo? , .

, , " void ?"

+3

All Articles