3

Some tutorials define void as a type, but in practice it is just a keyword, a placeholder to express "no type". So if we say that void is not a type practically, is that correct?

christo
  • 381
  • 2
  • 5
  • 2
    Looks like a dupe for [Is void a data type in C?](https://stackoverflow.com/questions/3487689/is-void-a-data-type-in-c) – Evg Oct 27 '20 at 09:06
  • its a type, in theory and practically. Its just a very special one because it has no values – 463035818_is_not_an_ai Oct 27 '20 at 09:07
  • 2
    Does this answer your question? [Is void a data type in C?](https://stackoverflow.com/questions/3487689/is-void-a-data-type-in-c) – atin Oct 27 '20 at 09:07
  • "void - type with an empty set of values. It is an incomplete type that cannot be completed (consequently, objects of type void are disallowed). There are no arrays of void, nor references to void. However, pointers to void and functions returning type void (procedures in other languages) are permitted." From [cppref](https://en.cppreference.com/w/cpp/language/types#Void_type) (it's also listed as a keyword) – George Oct 27 '20 at 09:07
  • A related discussion [What is the size of void?](https://stackoverflow.com/questions/1666224/what-is-the-size-of-void) – Lanting Oct 27 '20 at 09:07
  • Yes, void is a type in theory and practice. [Here](https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=vs-2019#void-type) you can find an official definition from Microsoft. – davide-pi Oct 27 '20 at 09:10
  • 1
    It's part of the type system, and listed among the built-in types in the standard. Granted, it's an irregular type, but a type still. – StoryTeller - Unslander Monica Oct 27 '20 at 09:12
  • Where `int` is {`INT_MIN`, …, -1, 0, 1, …, `INT_MAX`}, `unsigned int` is {0, 1, …, `UINT_MAX`}, `void` is ∅ or {}. A type doesn't need to be a non-empty set. – chris Oct 27 '20 at 09:17
  • Disagree with the closure, as `void` in C++ is subtly different. For instance, C needs it as the placeholder in `int foo(void);` whereas C++ has just `int foo();`. – MSalters Oct 27 '20 at 09:33
  • It is just a bit interesting that void is considered as a fundamental type, and variable is one of the most known fundamental data in programming, but you cannot use void for variables. – christo Oct 27 '20 at 09:33
  • When we use void as a function return type, i guess, that is a placeholder rather, because no data will be returned. In the case of pointers, void* can be considered as a type, but without operations. Are there any other use cases of void? – christo Oct 27 '20 at 09:36
  • @christo a funtion is a type and you can't have a variable of type function – bolov Oct 27 '20 at 09:37
  • Other use-cases for `void` are templates/metaprogramming. – t.niese Oct 27 '20 at 09:38
  • 1
    It's useful to have an uninhabited type in a type system. It is similar to zero in arithmetic. – molbdnilo Oct 27 '20 at 09:38
  • void is used to specify that the function doesn't return any value.main function is not void it returns int to the fuction who has called it but malloc function returns a void * so u have to cast it . Void has use to specify type. – Udesh Oct 27 '20 at 09:44

2 Answers2

4

The C++ Standard says that void is an incomplete type that cannot be completed (unlike other incomplete types that can be completed).

This means you cannot apply the sizeof operator to void, you can't declare a variable of type void, but you can have a pointer to an incomplete type, and declare a function that return it. In case of the declaration of a return type void,the function has no return.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35
  • Addendum. In a few situations, a `void Foo() { return void(); }` can be a useful construct. The situations are mostly templates and macros, but useful to be aware of the syntax. – Eljay Oct 27 '20 at 12:07
4

This is not correct. The keyword void can be used in many places where C++ requires a type, for instance in template arguments. It's syntactically not a placeholder there. Of course, a template author may still decide to treat void as meaning "no type"; it's a common interpretation.

throw expressions explicitly have void type, even though the keyword void is not used there.

Function call expressions can have void type, and the return statement in a void function can take a void expression.

MSalters
  • 173,980
  • 10
  • 155
  • 350