Talk:Void type

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Example[edit]

Although I did not agree with 71.85.145.203 just replacing the entire article with a simple example, it does beg the question, why not include an example (identifying the language used of course). For example, 71.85.145.203's C example is:

public void setJobTitle(String jobTitleIn) {
jobTitle=jobTitleIn;
}

I haven't written any software since C++ came out (when "Hello world!" coding went from 4 lines to 4 pages :-) but if this is a reasonable example, and we are in agreement in regard to adding an example, would someone add an example section in the article and drop this (or something better if you like) in there? cheers, JimScott 22:48, 20 November 2006 (UTC)[reply]

You can return a void value with a non-empty return statement[edit]

What about this?

#include <stdio.h>

void f(void)
{
  puts("in f.");
}

void g(void)
{
  puts("in g.");
  return f();
}

int main(void)
{
  g();
  return 0;
}

It compiles on gcc (last time I checked), and it works as expected. Should it? Degenerate case? -- 92.229.73.235 (talk) 07:05, 8 March 2010 (UTC)[reply]

Interesting. If you compile that with gcc with the flags
-std=c99 -pedantic -Wall -Wextra
it gives a warning, but still compiles and runs fine. However, the C99 Standard says (section 6.8.6.4 paragraph 1), "A return statement with an expression shall not appear in a function whose return type is void," which I take to mean that the code above does not conform to the standard but the gcc folks either didn't think of that special case or decided they didn't care. —Bkell (talk) 08:08, 8 March 2010 (UTC)[reply]
Also, section 6.3.2.2 of the C99 Standard:
The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)
Bkell (talk) 08:12, 8 March 2010 (UTC)[reply]

“Unrelated notion”[edit]

“C and C++ also support the pointer to void type (specified as void *), but this is an unrelated notion. Variables of this type are pointers to data of an unspecified type, so in this context (but not the others) void acts as a universal or top type.” That is not true, under many circumstances it acts like a top type, but you cannot instantiate variables of the type void or allocate voids:

void foo()
{
  (void)12;
  return 5;
}

(valid C++) How should that be reformulated? --Chricho ∀ (talk) 21:51, 10 March 2011 (UTC)[reply]

I know this is 4 years old, but just for posterity - it doesn't need to be reforumulated. The sentence only says that void acts as top in the context of void*, not generally. 82.20.88.133 (talk) 13:08, 11 March 2015 (UTC)[reply]

I don't understand the first sentence[edit]

This is the same explanation I see everywhere and I can't imagine what its supposed to mean. "The void type, " ...", is the type for the result of a function that does not return a result." A result that doesn't return a result? Doesn't return it where? Is this sentence supposed to be implying that a void-type-method is "a method which doesn't include the 'return' command"? I suggest a better opening sentence, one that explains the meaning better, rather than leaving so many unanswered questions. —Preceding unsigned comment added by 72.187.99.79 (talk) 18:56, 4 April 2011 (UTC)[reply]

Generic Programming[edit]

How about adding some information about void type vs. generic class or function (procedure)?

For example:

t voidTypeExample<t>() {
}

Since OOP languages aren't so flexible with working with pointers.

Galzigler (talk) 19:55, 12 March 2012 (UTC)[reply]

f(...) in C++[edit]

You cannot pass "any data" type to a function with this signature because it might trigger undefined behavior and crash at runtime i.e. to call f with a std::string as parameter. --RokerHRO (talk) 14:11, 23 July 2012 (UTC)[reply]

No. Such class type arguments are conditionally-supported with implementation-defined semantics since C++11. Other types may be ill-formed, but not UB. --幻の上帝 (talk) 08:00, 5 January 2016 (UTC)[reply]

No values?[edit]

The introduction says: (...) the void type is taken to be an empty type with no values.

In Algol 68, the VOID type has one value. It even has a denotation: EMPTY. Rp (talk) 14:16, 15 January 2013 (UTC)[reply]