MetaProgramming Just Might Save My Life

Started by
12 comments, last by pauls_1979 18 years, 4 months ago
Please don't take this the wrong way but that code is not template metaprogramming it's just ad hoc use of templates & macros. In general metaprogramming should be about reducing repetitive boilerplate code not increasing it and making code more declarative. The code is not flexible it looks very brittle to me.

Lastly no matter what solution you come up using purely templates for vertex declarators it will never be flexible enough, for instance you can't have custom vertex formats in a text file (or from some stream) that can be read into the program (not without erasing type info) it must be all in header-style files. If you want/need such flexiablity/extensibility you must scarifice some static typing & type safety.

Quote:Original post by arithma
... using void pointer arithmetic.


Thats funny since arithmetic on void pointers is illegal (but not for pointer to pointer to ... pointer to void)

[Edited by - snk_kid on December 2, 2005 6:50:37 AM]
Advertisement
Quote:
Please don't take this the wrong way but that code is not template metaprogramming it's just ad hoc use of templates & macros. In general metaprogramming should be about reducing repetitive boilerplate code not increasing it and making code more declarative. The code is not flexible it looks very brittle to me.

Lastly no matter what solution you come up using purely templates for vertex declarators it will never be flexible enough, for instance you can't have custom vertex formats in a text file that can be read into the program it must be all in header-style files. If you want/need such flexiablity/extensibility you must scarifice static typing & some type safety.

I guess you're right about it not being true meta-programming, but it works and is more than flexible enough for my purposes. Specifying the internals of a vertex format through text files might be a bit too ambitious for me at the moment, and if I have to add new fields to the vertex data I'll just have to change the header then re-compile my libs and tools I guess, this isn't likely to happen very often though so it should be ok. I can't foresee any other problems and I'm pretty happy with what I've got (no doubt a couple of months down the line I'll discover some inherent fault and change my mind but nevermind) but of course suggestions and constructive criticism are always welcome.

I would like to find a more elegant solution than having to write up template classes for each vertex data field (see below) but short of writing a macro I can't really think of a better way to do it.

template <> struct VertexDataSize<VERTEX_FORMAT_POS>
{
static const unsigned ms_size = sizeof(float) * 3;
};

I suggest looking at numeric_limits. I don't follow completely what you are doing but I think it could be done much simplier. It isn't even clear to me why you need the templates. It seems like the defines could just be the offsets and you would be done with it.

The advantage of templates is that you can operate on the type. You can literally say if it's this type do this. Yet you use these flags so you can't actually do anything with the type which pretty much negates the reason for using templates. I know the mindset on this is metaprogramming and in some places you are metaprogramming, but it doesn't really seem a meta programming problem. You could use something like numeric limits to give you those sizes and offsets. You could have a typedef of class scope to tell you which comes before and after which. I think you really need to rethink that template since you're losing your type checking which not having to do that is a big advantage of templates.
Keys to success: Ability, ambition and opportunity.
For anyone who's still interested I'm currently looking through the article in GameDeveloper mag - "Taming Vertex Data Using C++ Templates" - recommended by ghostd0g. It's very good and looks like a much more comprehensive solution than what I came up with. I'll use my stock excuse of being new to C++, I think I'm still a C programmer at heart and it's a hard habit to break.

This topic is closed to new replies.

Advertisement