Thread overview
Printing type name in static assert
Jan 25, 2014
Martin Cejp
Jan 25, 2014
Rikki Cattermole
Jan 25, 2014
Jason White
Jan 25, 2014
Jason White
Jan 25, 2014
Dicebot
Jan 25, 2014
Martin Cejp
Jan 25, 2014
Martin Cejp
January 25, 2014
I'm trying to do something to this effect:

template XYZ(Class) {
...
static assert(index != -1, "No such annotation on " ~ typeid(Class));
...
}

However, typeid is not a string and I can't do to!string. Nor is typeid().name implemented at compile time.
What would be the correct way to do this?
January 25, 2014
On Saturday, 25 January 2014 at 11:28:40 UTC, Martin Cejp wrote:
> I'm trying to do something to this effect:
>
> template XYZ(Class) {
> ...
> static assert(index != -1, "No such annotation on " ~ typeid(Class));
> ...
> }
>
> However, typeid is not a string and I can't do to!string. Nor is typeid().name implemented at compile time.
> What would be the correct way to do this?

Class.stringof perhaps?
January 25, 2014
I think what you are looking for is Class.stringof:

    template XYZ(Class)
    {
        // ...
        static assert(index != -1,
            "No such annotation on "~ Class.stringof)
            );
        // ...
    }
January 25, 2014
On Saturday, 25 January 2014 at 11:41:54 UTC, Rikki Cattermole
wrote:
> ...

Damn, beaten by 1 second!
January 25, 2014
On Saturday, 25 January 2014 at 11:28:40 UTC, Martin Cejp wrote:
> I'm trying to do something to this effect:
>
> template XYZ(Class) {
> ...
> static assert(index != -1, "No such annotation on " ~ typeid(Class));
> ...
> }
>
> However, typeid is not a string and I can't do to!string. Nor is typeid().name implemented at compile time.
> What would be the correct way to do this?

for just printing `Class.stringof` is most simple thing to do. There are also `__traits(identifier, Class)` and `std.traits.fullyQualifiedName!Class` that are more specialized and usually used in code generation.
January 25, 2014
On Saturday, 25 January 2014 at 11:44:04 UTC, Dicebot wrote:
> On Saturday, 25 January 2014 at 11:28:40 UTC, Martin Cejp wrote:
>> I'm trying to do something to this effect:
>>
>> template XYZ(Class) {
>> ...
>> static assert(index != -1, "No such annotation on " ~ typeid(Class));
>> ...
>> }
>>
>> However, typeid is not a string and I can't do to!string. Nor is typeid().name implemented at compile time.
>> What would be the correct way to do this?
>
> for just printing `Class.stringof` is most simple thing to do. There are also `__traits(identifier, Class)` and `std.traits.fullyQualifiedName!Class` that are more specialized and usually used in code generation.

Thanks, that will do.
fullyQualifiedName was the first that came to mind, but it's broken.
January 25, 2014
alias Field = typeof(__traits(getMember, Vertex, fieldName));

const bool normalized = HasAnnotation!(Vertex, fieldName, Normalized);
const int fieldOffset = __traits(getMember, Vertex.init, fieldName).offsetof;

int i = GetAnnotationInstance!(Vertex, fieldName, Position).pos;

glVertexAttribPointer(i, cast(GLint) numElements!Field, TypeToGLEnum!(ElementType!Field),
        normalized ? GL_TRUE : GL_FALSE, Vertex.sizeof, cast(void*) fieldOffset);
glEnableVertexAttribArray(i);


I'm starting to like this language :-)