Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construction of a void Type?

Tags:

c++

void

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:

  1. Writing the function void askVoid(void param) {} errors:

A parameter may not have void type

  1. Writing the function void askNaught() {} and calling it with askNaught(void())` errors:

error C2660: takeNaught: function does not take 1 arguments

  1. Writing the templatized function template <typename T> void takeGeneric(T param) {} and calling it with takeGeneric(void()) errors:

error C2893: Failed to specialize function template void takeGeneric(T)

  1. Declaring void voidType errors:

Incomplete type is not allowed

  1. Declaring auto autoVoid = void() errors:

Cannot deduce auto type

  1. Declaring void* voidPtr works fine, but remove_pointer_t<decltype(voidPtr)> decltypeVoid errors:

error C2182: decltypeVoid: illegal use of type void

That's it, right? There is no place for void() in C++ is there? This is just bad code I've been given, right?

like image 462
Jonathan Mee Avatar asked Jun 08 '16 11:06

Jonathan Mee


People also ask

What is a void type in programming?

The void type, in several programming languages derived from C and Algol68, is the type for the result of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects, such as performing some task or writing to their output parameters.

What does () => void mean TypeScript?

Introduction to TypeScript void type The void type denotes the absence of having any type at all. It is a little like the opposite of the any type. Typically, you use the void type as the return type of functions that do not return a value.

What is the void type in C++?

When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters.

What is a void return type?

A void return type simply means nothing is returned. System. out. println does not return anything as it simply prints out the string passed to it as a parameter.


5 Answers

C++ (and I say C++, not C) allows (§6.6.3 comma 2) functions with void return type to return a void expression, that is:

void foo() { return void(); }

But notice it is not constructing a temporary void!

like image 86
edmz Avatar answered Sep 30 '22 19:09

edmz


The expression void() is a prvalue of type void and can be used anywhere such an expression may be used, which [basic.fundamental]/9 helpfully provides a list:

  • As an expression-statement: void();
  • As the second or third operand of a conditional operator: true ? throw 1 : void()
  • As an operand of the comma operator: ++it1, void(), ++it2
  • As the operand of decltype or noexcept: using my_void = decltype(void()); static_assert(noexcept(void()), "WAT");
  • In a return statement of a function returning (possibly cv-qualified) void: const void f() { return void(); }
  • As an operand of an explicit conversion to (possibly cv-qualified) void: static_cast<const void>(void())

An expression of type void can also be used as the operand of typeid, but void() in particular would be parsed as a type, not an expression, in this context.

like image 38
T.C. Avatar answered Oct 01 '22 19:10

T.C.


You can take a void() as function parameter:

void test(void()) { ... }

Which expands to:

void test(void (*)())

Which is a function pointer to a method which returns void and takes no arguments.

Full example:

void abc() {}
void test(void()) { }

int main() {
    test(abc);
}
like image 20
tobspr Avatar answered Oct 03 '22 19:10

tobspr


You can use void() as a callable type, as an example std::function<void()> f; is a valid statement.

Moreover, as from 8.3.5/4:

A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list.

That means that this is valid:

template<typename T>
struct F;

template<typename R, typename... A>
struct F<R(A...)> { };

int main () {
   F<void(void)> s;
}

Here you are not instantiating anything of type void, but you are using it (let me say) as a parameter list of a callable type.

Not sure if this replies to your question, I've not clear what the question actually is.

like image 36
skypjack Avatar answered Oct 02 '22 19:10

skypjack


There is no place for void() in C++ is there?

As an expression, void() is valid in C++.

From the standard, $5.2.3/2 Explicit type conversion (functional notation) [expr.type.conv]:

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, whose value is that produced by value-initializing (8.5) an object of type T; no initialization is done for the void() case.

From cppreference.com:

new_type ( )

If new_type is an object type, the object is value-initialized; otherwise, no initialization is done. If new_type is (possibly cv-qualified) void, the expression is a void prvalue.

like image 1
songyuanyao Avatar answered Oct 01 '22 19:10

songyuanyao