On Tue, 22 Sep 2009 19:40:03 -0400, Jeremie Pelletier <jerem...@gmail.com> wrote:

Christopher Wright wrote:
Jeremie Pelletier wrote:
Why would you declare void variables? The point of declaring typed variables is to know what kind of storage to use, void means no storage at all. The only time I use void in variable types is for void* and void[] (which really is just a void* with a length).

In fact, every single scope has an infinity of void variables, you just don't need to explicitly declare them :)

'void foo;' is the same semantically as ''.
It simplifies generic code a fair bit. Let's say you want to intercept a method call transparently -- maybe wrap it in a database transaction, for instance. I do similar things in dmocks.
 Anyway, you need to store the return value. You could write:
 ReturnType!(func) func(ParameterTupleOf!(func) params)
{
    auto result = innerObj.func(params);
    // do something interesting
    return result;
}
 Except then you get the error: voids have no value
So instead you need to do some amount of special casing, perhaps quite a lot if you have to do something with the function result.

I don't get how void could be used to simplify generic code. You can already use type unions and variants for that and if you need a single more generic type you can always use void* to point to the data.

Besides in your above example, suppose the interesting thing its doing is to modify the result data, how would the compiler know how to modify void? It would just push back the error to the next statement.

Why don't you just replace ReturnType!func by auto and let the compiler resolve the return type to void?

Because auto returns suffer from forward referencing problems :

//Bad
auto x = bar;
auto bar() { return foo; }
auto foo() { return 1.0; }

//Okay
auto foo() { return 1.0; }
auto bar() { return foo; }
auto x = bar;

Reply via email to