Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:26:45+00:00 2026-05-26T20:26:45+00:00

As explained, for example, here , there are 3 main uses for the void

  • 0

As explained, for example, here, there are 3 main uses for the void keyword (more experienced C/C++ programmers can skip to the 4th use):

1) As a return type for function that doesn’t return anything. This
will cause a code sample like this:

void foo();
int i = foo();

to generate a compiler error.

2) As the only parameter in a function’s parameter list. AFAIK, an empty function’s parameter list is exactly the same to the compiler and therefore the following 2 lines are identical in meaning:
(edit: it is only true in c++. The comments show the difference in c).

int foo();
int foo(void);

3) void* is a special type of generic pointer– it can point to any variable that is not declared with the const or volatile keyword, convert to/from any type of data pointer, and point to all non-member functions. In addition, it cannot be dereferenced. I will not give examples.

There is also a 4th use that I don’t fully understand:

4) In conditional compilation it is often used in the expression (void)0 as following:

// procedure that actually prints error message 
void _assert(char* file, int line, char* test); 
#ifdef NDEBUG 
#define assert(e) ((void)0) 
#else
#define assert(e)     \
((e) ? (void)0 :   \
__assert(__FILE__, __LINE__, #e)) 
#endif

I’m trying to understand the behavior of this expression through experiments. All the following are valid (compile well):

int foo(); // some function declaration
int (*fooPtr)(); // function pointer
void(foo);
void(fooPtr);
void(0);
(void)0;
void('a');
void("blabla");
exampleClass e; //some class named exampleClass with a default ctor
void(e);
static_cast<void>(e);

but these are not:

void(0) // no semicolon
int i = void(0);

Can I conclude from this that “void” (in the context of the 4th use) is simply a special type that any type can cast to it (whether it is c-style or cpp-style), and it can never be used as an lvalue or rvalue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:26:46+00:00Added an answer on May 26, 2026 at 8:26 pm

    Can I conclude from this that "void" (in the context of the 4th use) is simply a special type that any type can cast to it (whether it is c-style or cpp-style), and it can never be used as an lvalue or rvalue?

    James McNellis pointed out in a comment above that void can be used as an rvalue via the expression void().

    He quoted this from the current C++ standard:

    C++11 §5.2.3/2:
    “The expression T(), where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, which is value-initialized (no initialization is done for the void() case).”

    This makes it possible to write code like …

    template< class T >
    T foo() { return T(); }
    

    and use foo<void>() (most likely from other templated code, I would imagine).

    Formally void is just an incomplete type that can never be completed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably explained more easily with an example. I'm trying to find a
Probably best explained with an example: struct A { virtual void foo() = 0;
Best explained with code I think, this is just a simple example: public class
Let me use the following example to explain my question: public string ExampleFunction(string Variable)
I need to explain this by example: Is there a best practice or preference
Can anyone explain to me one or more efficient, reliable methods for opening a
Can anyone explain to me one or more efficient, reliable methods for opening a
There's example HTTP Client at http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/example/http/client/async_client.cpp Please help me to change maximum buffer size
Here is an example of what I am trying to do. XML: <TEST> <NODE>
Can someone explain why the HashMap acts like it does in this example: Simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.