I have a bunch of types that have a name. (They have more features, but for the sake of this discussion only the name is relevant.) These types and their names are setup at compile-time using a macro:
#define DEFINE_FOO(Foo_) \
struct Foo_ : public foo_base<Foo_> { \
static char const* name() {return #Foo_;} \
}
The types are then combined in compile-time lists (classic simple recursive compile-time lists), from which I need to create the list's name by concatenating the names of its objects:
template<class Foo, class Tail = nil>
struct foo_list {
static std::string name_list() {return Foo::name() + "-" + Tail::name();}
};
template<class Foo>
struct foo_list<Foo,nil> {
static std::string name_list() {return Foo::name();}
};
The code is boiled down here to the point where it might contain errors, but in practice this works pretty well.
Except that it creates and then copies around rather long strings at runtime which represent types that actually are well-known at compile-time. Since this is a rather performance-sensitive piece of code that runs on embedded devices, I'd like to change this so that
- the list's string is ideally created at compile-time, or, if there's no way to do that, once at runtime, and
- I only need to copy around a pointer to a C string, since, according to #1, the strings are fixed in memory.
- This compiles with C++03, which we're stuck with right now.
How can I do this?
(In case this enlarges the arsenal of dirty tricks employable for this: The names of the foo
objects are only ever created and read by code, and only the foo_list
name strings are expected to be human-readable.)
typeid
? – Welcomefoo
template are known at compile time, it is rather trivial to even add a compile-time constant of the string's length. Why are you asking? – Assemblage