Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Meta-programming with Char Arrays as Parameters

I'm playing around with TMP in GCC 4.3.2's half-implementation of C++11, and I was wondering if there was a way to somehow do the following:

template <char x, char... c>
struct mystruct {
...
};

int main () {

   mystruct<"asdf">::go();

}

It obviously won't let me do it just like that, and I thought I'd get lucky by using user-defined literals to transform the "asdf" string during compile-time, but GCC 4.3 doesn't support user-defined literals...

Any suggestions? I'd prefer to not do 'a','s','d','f', since this severely hampers my plans for this project.

like image 520
Daniel Jennings Avatar asked Apr 02 '09 21:04

Daniel Jennings


1 Answers

I solved a problem similar to this. We needed a different type for each name

template< const char* the_name >
class A
{
    public:
    const char* name( void )
    {
        return the_name;
    }
};

extern const char g_unique_name[]; // defined elsewhere
typedef A<g_unique_name> A_unique;

This will gave you compile time access to the name, and unique instantiation. It however will not let you access the individual characters at run time.

If you want individual character access the only way to achieve it is with a user defined literal. C++0x will be extended to allow the syntax in your main function above but it will still bind the template to a character pointer not to a compile time array of characters.

like image 76
deft_code Avatar answered Sep 28 '22 04:09

deft_code