Is there any way to differentiate between a type and an alias?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 25 07:40:06 PDT 2014


On Sun, 25 May 2014 04:04:09 -0700, Rene Zwanenburg  
<renezwanenburg at gmail.com> wrote:

> Given
>
> alias GLenum = uint;
> void glSomeFunction(GLenum, uint);
>
> Now, is there some way to differentiate between GLenum and uint when  
> using ParameterTypeTuple!glSomeFunction?
>
> I'm writing a function which shows the arguments a GL function was  
> called with when an error occurs. The GLenum needs to be printed as a  
> stringified version of the constant's name, while the uint is just an  
> uint.

An alias is simply another name for the same thing. There is no type  
difference.

You may be able to do some template trickery with template aliases to  
detect when an alias is used. But I'd recommend using enum instead of  
alias:

enum GLenum : uint { constant = value}

This creates a genuine new type, and also gives you a place to put  
constants. However, it's not implicitly castable from uint, so it has some  
drawbacks. You can cast back to uint implicitly though.

There is also a library typedef mechanism (in std.typecons perhaps?), you  
can look into that. It should have the same limitations as enum.

-Steve


More information about the Digitalmars-d-learn mailing list