Daniel Keep wrote:

Jeremie Pelletier wrote:
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.

You can't take the address of a return value.  I'm not even sure you
could define a union type that would function generically without
specialising on void anyway.

And using a Variant is just ridiculous; it's adding runtime overhead
that is completely unnecessary.

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.

Example from actual code:

ReturnType!(Fn) glCheck(alias Fn)(ParameterTypeTuple!(Fn) args)
{
    alias ReturnType!(Fn) returnT;

    static if( is( returnT == void ) )
        Fn(args);
    else
        auto result = Fn(args);

    glCheckError();

    static if( !is( returnT == void ) )
        return result;
}

This function is used to wrap OpenGL calls so that error checking is
performed automatically.  Here's what it would look like if we could use
void variables:

ReturnType!(Fn) glCheck(alias Fn)(ParameterTypeTuple!(Fn) args)
{
    auto result = Fn(args);

    glCheckError();

    return result;
}

I don't CARE about the result.  If I did, I wouldn't be allowing voids
at all, or I would be special-casing on it anyway and it wouldn't be an
issue.

The point is that there is NO WAY in a generic function to NOT care what
the return type is.  You have to, even if it ultimately doesn't matter.

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

Well, there's this thing called "D1".  Quite a few people use it.

Especially since D2 isn't finished yet.

Oops sorry! I tend to forget the semantics and syntax of D1, I haven't used it since I first found about D2!

I would have to agree that you do make a good point here, void values could be useful in such a case, so long as the value is only assigned by method calls and not modified locally.

Basically in your example, auto result would just mean "use no storage and ignore return statements on result if auto resolves to void, but keep the value around until I return result if auto resolves to any other type".

Jeremie

Reply via email to