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
17
votes
2 answers

Is void() a valid C++ expression?

Apparently, this ternary expression with void() as one argument compiles: void foo() {} //... a == b ? foo() : void(); Is void() a valid expression by the standard, or is it just a compiler thing? If it's valid, then what kind of expression is it?
petiaccja
  • 171
  • 6
17
votes
2 answers

How does int main() and void main() work?

I am a beginner in the C language. Can anyone explain in detail using example how main(), int main(), void main(), main(void), void main(void), int main(void) work in C language? As in, what is happening when we use void main() and what is happening…
user2106271
  • 179
  • 1
  • 1
  • 3
17
votes
11 answers

If void() does not return a value, why do we use it?

void f() means that f returns nothing. If void returns nothing, then why we use it? What is the main purpose of void?
17
votes
6 answers

What's the difference between static inline void and void?

I'm working in the C language and modifying code previously written by someone else. I'm struggling with a few things and I'm trying to understand as much as I can about what is going on as I can. So, as my question stated, what is the difference…
TZPike05
  • 1,188
  • 5
  • 15
  • 24
16
votes
1 answer

Error calling 'Void' on 'onPressed/onTap' Flutter

I get an error calling a void in onPressed/onTap I'm trying to call a set state in an Inherited widget/state following this instructions https://medium.com/flutter-community/widget-state-buildcontext-inheritedwidget-898d671b7956 To my inexpert…
Francesco Iapicca
  • 2,618
  • 5
  • 40
  • 85
16
votes
7 answers

In Java, can "void" be considered a primitive type?

I've noticed eclipse JDT uses void as a primitive type. Can this be considered correct?
John Assymptoth
  • 8,227
  • 12
  • 49
  • 68
16
votes
5 answers

Calling a non-void function without using its return value. What actually happens?

So, I found a similar question here, but the answers are more about style and whether or not you are able to do it. My question is, what actually happens when you call a non-void function that returns an object, but you never assign or use said…
DomenicDatti
  • 655
  • 7
  • 15
16
votes
1 answer

Is std::declval() a valid expression?

As far as I know, I cannot declare an rvalue reference to void. As an example, the following code is ill-formed: void f(void &&v) { } From [20.2.6/1] (function template declval) we have a declaration for declval that is: template
skypjack
  • 49,335
  • 19
  • 95
  • 187
16
votes
3 answers

Variables of type void

So I know that Void is used to indicate that a function doesn't take or return a value. In Swift, Void is actually a type alias for the empty tuple (). Interestingly (in Beta 6), you can even declare variables of type Void: var x: Void println(x) x…
Atomix
  • 13,427
  • 9
  • 38
  • 46
15
votes
1 answer

casting 0 to void

On my implementation of C++ (Visual Studio 2008 implementation) I see the following line in #ifdef NDEBUG #define assert(_Expression) ((void)0) I do not understand the need to cast 0 to void. It seems to me that #ifdef NDEBUG #define…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
15
votes
2 answers

In Jest, how do I mock a Promise of void?

I'm using Jest and Typescript. I have a async function that returns nothing (void). How do I mock returning void? I tried the below const myMockFn = jest.fn().mockImplementationOnce(() => Promise.resolve(void)); jest.mock('../config', () => ({ …
Dave
  • 15,639
  • 133
  • 442
  • 830
15
votes
7 answers

Why can't objects of type void be created in C++?

C++ doesn't allow creating objects of type void. Why is that?
rakzz
  • 213
  • 1
  • 3
  • 12
15
votes
2 answers

Why (void) between two comma separated statements in a for loop

The following code comes from an implementation example of std::lexicographical_compare on cppreference.com: template bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2…
Vincent
  • 57,703
  • 61
  • 205
  • 388
15
votes
2 answers

Casting pointer to void

Is there any difference in below two castings ? int a=10; int *p=&a; (void)p; //does not give any warning or error or (void *)p; //error: statement with no effect [-Werror=unused-value] when complied with gcc -Wall -Werror…
ameyCU
  • 16,489
  • 2
  • 26
  • 41
15
votes
1 answer

isn't non-type parameter pack that evaluates to "void..." illegal?

gcc-4.8 accepts this code, but isn't it wrong since the non-type parameter pack is equivalent to void... which is illegal? template ::value>::type...> void test(T) {} I tried this…
Hui
  • 337
  • 2
  • 5