45

I'm looking for a formal explanation of that fact in the Standard. I've found what 3.9.1/9 says and trying to give an explanation used that section.

Section 3.9.1/9, N3797:

The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void (5.4). An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of typeid, noexcept, or decltype, as the expression in a return statement (6.6.3) for a function with the return type void, or as the operand of an explicit conversion to type cv void.

I don't understand how it implies from the fact that the void type has an empty set of values.

We can declare a variable of incomplete type in the following way (DEMO):

struct Foo;
extern Foo f; // OK

However, this cannot be done with void, which is also incomplete (DEMO):

extern void f; // error

Why does the compiler raise an error in this case?

19
  • 12
    If the type has an empty set of values, what can you do with the variable?
    – Barmar
    Sep 15, 2014 at 17:11
  • 3
    They key is "The void type is an incomplete type that cannot be completed", that's what makes it different than any other incomplete type.
    – K-ballo
    Sep 15, 2014 at 17:33
  • 1
    @K-ballo: struct Foo is an incomplete object type. void is not an object type. That makes it different in a very fundamental way.
    – Ben Voigt
    Sep 15, 2014 at 17:40
  • 5
    @K-ballo: Because the definition of object type says it isn't.
    – Ben Voigt
    Sep 15, 2014 at 17:45
  • 1
    @BenVoigt: implicit conversion to uintptr_t would enable some weird expressions though, such as ptr + 1.0. Makes sense to me to have a type for "address of something unspecified", especially in C. But if you're going to make that an integer type I think you should require a cast in both directions. A pointer type that isn't spelled void* might satisfy all quibbles. Sep 15, 2014 at 21:36

7 Answers 7

27

You cannot declare a variable of type void because variables must have object type or be references, extern void f; doesn't declare a reference, and void is not an object type:

Section 3 [basic] says that

A variable is introduced by the declaration of a reference other than a non-static data member or of an object.

Section 3.9 [basic.types] says that

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.

8
  • 1
    So? Where does 7.1 state that object types are necessary?
    – MSalters
    Sep 15, 2014 at 18:09
  • @MSalters: 7.1 covers many different declarations. Object types are only necessary for declarations of objects. But I don't think Dmitry is claiming that extern void f; should declare a function or a reference. If he did claim that, it would be pretty straightforward to show that void is also not a function type and not a reference type.
    – Ben Voigt
    Sep 15, 2014 at 18:46
  • @MSalters: Besides, I think it is section 8 that controls declarations. The first sentence "A declarator declares a single variable, function, or type, within a declaration." And from section 3: "A variable is introduced by the declaration of a reference other than a non-static data member or of an object"
    – Ben Voigt
    Sep 15, 2014 at 18:47
  • The latter part is easiest. The fact that variables are introduced by a declaration doesn't mean that everything declared is a variable. Section 8 covers declarators, which are the "non-type parts" of a declaration. See 8/1. But the void here is a type-specifier, in particular a simple-type-specifier (7.1.6.2).
    – MSalters
    Sep 15, 2014 at 20:00
  • @MSalters: Right, and it could appear in (1) a typedef, (2) a function declaration, where it would serve as the return type and the declarator must contain (), (3) a reference declaration, where the declarator must contain &, (4) a variable declaration of pointer type, where the declarator contains *. The syntax definitely allows void to be a type-specifier. But the rules do not allow declaring a variable whose type is void (alone).
    – Ben Voigt
    Sep 15, 2014 at 20:15
8

"void type is an incomplete type"

You can't create variables of any incomplete type

"...that cannot be completed"

While your example of extern incomplete struct can be completed at some later point, the compiler knows that any declaration of type void can never be completed.

2
  • 4
    We can't define an object of incomplete type, but can declare a variable of such types. coliru.stacked-crooked.com/a/55b2c751ba86478f
    – user2953119
    Sep 15, 2014 at 17:14
  • 3
    Note, that the post is not an answer what I'm looking for. Because I asked about declaration, not definition. Object definition of incomplete type denied by 3.9/5.
    – user2953119
    Sep 15, 2014 at 17:15
3

[edit] The answer below makes valid observations, but they're contradicting. As these might be valuable, I'll not delete them, but see Ben Voight's answer and the comments there for a more straightforward approach.

Your observations about extern declarations are specifically allowed by 7.1.1/8:

The name of a declared but undefined class can be used in an extern declaration. Such a declaration can only be used in ways that do not require a complete class type.

void is not a "declared but undefined class", and there's no other exception in 7.1.1 which applies.

Additionally, 3.9/5 is fairly explicit that it is in fact allowed:

A class that has been declared but not defined, an enumeration type in certain contexts (7.2), or an array of unknown size or of incomplete element type, is an incompletely-defined object type. [45] Incompletely defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.

Emphasis mine. This part of the standard is quite specific about the differences between definitions and declarations, so by omission it specifies that declarations are allowed.

3
  • I thought you can also declare extern variables e.g. of array types with unknown bounds?
    – dyp
    Sep 15, 2014 at 18:06
  • Interesting. @dyp isn't int x[] just a int* however? Sep 15, 2014 at 18:10
  • @Yakk Nope, look at [basic.types]/6. Such an incomplete type can be completed: extern int arr[]; /* ... */ int arr[10]; (outside of function parameters, of course)
    – dyp
    Sep 15, 2014 at 18:11
2

If the variable has an empty set of values, it can't be used for anything.

You can't assign to it, because there are no possible values to assign.

You can't access it, because you never assigned to it, so it has an indeterminate value.

Since there are no possible values, there's no size of the variable.

void is just used as a placeholder in variable places. It's used as a return type to indicate that the function doesn't return a value. It's used in C in the argument list to indicate that the function takes no arguments (to resolve an ambiguity from the pre-prototype version of the language). And it's used with pointer declarations to create generic pointers that can be translated to any other pointer type. There's no such analogous use for it in variable declarations.

5
  • Could you look at my updated answer. I clarified what exact I want to understand.
    – user2953119
    Sep 15, 2014 at 17:20
  • 2
    An extern declaration requires that one of the other modules being linked has a definition of the variable. But there can't be a module that defines a void variable, because what would it put in it?
    – Barmar
    Sep 15, 2014 at 17:26
  • 2
    So my tag struct my_tag {}; is similarly useless. Yet, I can define an object of its type, I can assign to it, it has a nonzero size etc.
    – dyp
    Sep 15, 2014 at 18:09
  • What can you assign to it?
    – Barmar
    Sep 15, 2014 at 18:43
  • 2
    A new "value"/object of the same type? my_tag x; x = my_tag{};
    – dyp
    Sep 15, 2014 at 18:45
1

Because C and C++ assume that any objects may be compared for identity by comparing their addresses, they must ensure that all objects have fixed non-zero size. Were it not for that requirement, there are in fact many cases where it would be somewhat useful to declare zero-sized objects [e.g. in code which uses templates which contain fields that will sometimes be useful and sometimes not, or as a means of forcing a structure to be padded to a certain alignment requiring that it contain an element requiring such alignment]. As it is, however, zero-size types would be inconsistent with fact that the rule specifying that every object has a unique address includes no exception which would allow for the existence of zero-sized objects that could share an address.

Even if zero-size objects were permissible, however, a "pointer to unknown object" should not be the same as a "pointer to a zero-size object". Given that the type void* is used for the former, that would imply that something else should be used for the latter, which would in turn imply that something other than void should be the type of thing to which a zero-sized object points.

6
  • 1
    I think this belongs on a discussion of the empty base class optimization; it has nothing to do with void.
    – Ben Voigt
    Sep 15, 2014 at 22:42
  • 1
    @BenVoigt: When someone asks "why is X not allowed in language Y", the "direct" answer is almost always "because the spec for Y says so", but that's generally not very informative or interesting. I think it is more informative in many cases to examine whether X could be allowed in any useful way without causing problems; in many cases where something is forbidden, it is to to avoid at least some problems that would be caused by allowing it.
    – supercat
    Sep 15, 2014 at 22:53
  • 1
    But all you prove is that (sizeof f) >= 1. Which can easily be satisfied by making 1 == sizeof (void) just like is already done for sizeof (empty_struct).
    – Ben Voigt
    Sep 15, 2014 at 23:03
  • @BenVoigt: If the rules about object uniqueness had allowed for zero-sized types, and if the pointer-to-anything type was called something like pointer rather than void*, then a zero-size void could have a semantic meaning distinct from an empty structure even if the latter also had zero size. I don't see any useful semantic meaning for a void type that takes up space, though.
    – supercat
    Sep 15, 2014 at 23:25
  • 1
    In other words, if you take all the behavior that void has now, and create new keywords for it, you could give void a brand new meaning, and change the language to make it useful. But how does this help us understand the void we have in the language we have?
    – Ben Voigt
    Sep 16, 2014 at 1:44
1

void is an incomplete type - you can only declare pointers to them and use them in function signatures. Obviously, extern Foo f; is permitted because struct Foo can be defined in another compilation unit (and if it's not the error will be detected by the linker), but void can't ever be "defined" (and the compiler knows this, of course) so void's quite special in this case.

3
  • 1
    you can _only_ declare pointers and use them in function signatures. It is not strictly speaking right. Because I've cited an exaple which declares a variable of incoplete type.
    – user2953119
    Sep 15, 2014 at 17:24
  • @DmitryFucintv ok, but it can never be completely specified as struct Foo; can, so it's special that way
    – Paul Evans
    Sep 15, 2014 at 17:27
  • @Yakk thanks, added something of that (hopefully) addresses that in the answer
    – Paul Evans
    Sep 15, 2014 at 23:16
1

Well - I really don't see the rationale behind this. It's going to be great if this way we can declare a variable with unknown type. Something like 'void *' and arrays of unknown size. Imagine code like this:

#include <iostream>
#include <cstring>

using namespace std;

extern void f;

int main()
{
    cout << (int &)f << endl; //cout 'f' as it was integer
}

struct {
    int a;
    double b;
} f{};

You can now actually do something similar with arrays:

#include <iostream>
#include <cstring>

using namespace std;

struct Foo;

extern int arr[];

int main()
{
    cout << arr[2] << endl;
}

int arr[4]{};

Life example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.