Questions tagged [void]

An incomplete type used as syntactic place-holder for the return type of a method/function when no value is returned.

Programming languages derived from C or Algol68, like C++, C#, Java, etc., may define the return type of methods/functions as void when the method/function does not return a value, but simply completes execution.

An example of its use in Java, compared to a non-void method, is:

int nonVoidMethod {
    // do something
    return 0; // return a value to the caller
}

void voidMethod {
    // do something
    return; // no value allowed to be returned from a void method
    // a "return" statement is not required
}

Although void is used as a type, it's an incomplete type:

  • In some languages, such as Java and Algol68, void is only a keyword used as a return type. It is not considered as a valid type in other language constructs.

  • Other languages, like C and C++, consistently define void as a type with an empty set of values. This allows its use for example in compound type constructs (void pointers). But no void objects can be instantiated.

1913 questions
28
votes
2 answers

Is void{} legal or not?

This is a follow-up up of this question. In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid expression. That was fine, it made sense and that was all. Then I came through [7.1.7.4.1/2]…
skypjack
  • 49,335
  • 19
  • 95
  • 187
28
votes
7 answers

printf("%p") and casting to (void *)

In a recent question, someone mentioned that when printing a pointer value with printf, the caller must cast the pointer to void *, like so: int *my_ptr = .... printf("My pointer is: %p", (void *)my_ptr); For the life of me I can't figure out why.…
zmbq
  • 38,013
  • 14
  • 101
  • 171
27
votes
3 answers

Is Void really uninstantiable?

The javadoc for Void says: The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void. but the constructor is simply: private Void() {} and this code instantiates a…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
26
votes
3 answers

Returning a void?

I do not understand why this code compiles without error: #include template struct Test { static constexpr T f() {return T();} }; int main() { Test test; test.f(); // Why not an error? return 0; } Is it…
Vincent
  • 57,703
  • 61
  • 205
  • 388
25
votes
3 answers

C programming: casting a void pointer to an int?

Say I have a void* named ptr. How exactly should I go about using ptr to store an int? Is it enough to write ptr = (void *)5; If I want to save the number 5? Or do I have to malloc something to save it?
Tim
  • 4,295
  • 9
  • 37
  • 49
24
votes
4 answers

What does (void)var actually do?

Consider the following main(): int main(int argc, char *argv[]) { return (0); } Upon compilation with cc -Wall -Wextra, warnings saying "unused parameter" get generated. When I do not need to use a parameter in a function (for instance in a…
Diti
  • 1,454
  • 3
  • 23
  • 38
24
votes
3 answers

What does—or did—"volatile void function( ... )" do?

I've seen How many usage does "volatile" keyword have in C++ function, from grammar perspective? about use of the volatile keyword on functions, but there was no clear explanation of what Case 1 from that question did. Only a statement by one of…
EdwinW
  • 1,007
  • 2
  • 13
  • 32
23
votes
4 answers

Does "instanceof Void" always return false?

Can this method return true somehow? public static boolean isVoid(T t) { return t instanceof Void; }
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
23
votes
2 answers

How much existing C++ code would break if void was actually defined as `struct void {};`

void is a bizarre wart in the C++ type system. It's an incomplete type that cannot be completed, and it has all sort of magic rules about the restricted ways it can be employed: A type cv void is an incomplete type that cannot be completed; such a…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
23
votes
5 answers

Construction of a void Type?

I was given a piece of code that uses void() as an argument. The code doesn't compile... obviously? Can we instantiate anything of type void? I believed the answer was no, with the exception of a void*. For example: Writing the function void…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
22
votes
4 answers

stubbing methods that manipulates parameters with mockito

I have the following situation: class Worker { public Integer somework() { Integer k=0; Helper h= new Helper(); h.change(k); return k; } } class Helper { public void change(Integer k) { //k = Some…
TryHarder
  • 289
  • 1
  • 4
  • 10
22
votes
1 answer

Observable usage pattern in Typescript

When an async service has no return value but I want to use Observables I'm inclined to use Observable. But I don't have meaning for this boolean value because the service either fails or succeeds and if it fails I want the Observable to be…
Halt
  • 2,924
  • 4
  • 22
  • 26
21
votes
4 answers

Flutter "this function has a return type of void and cannot be used"

I am getting this error: - This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and…
Debankush Mridha
  • 247
  • 1
  • 5
  • 10
21
votes
1 answer

The void(), the comma operator (operator,) and the impossible (?) overloading

Consider the following struct: struct S {}; In C++14, the definition below is valid: constexpr auto f() { return S{}, 'c'; } As well as the following one: constexpr auto f() { return S{}, void(); } Now, consider the following, working snippet…
skypjack
  • 49,335
  • 19
  • 95
  • 187
21
votes
4 answers

Mockito: how to stub void methods to run some code when called

I want to stub a repository class to test another class (Holder class) that has a repository. The repository interface supports CRUD operations, and has many methods, but my unit test on the Holder class only needs to call two of them. The…
Mister Smith
  • 27,417
  • 21
  • 110
  • 193