It is very long standing problem of C++ that you cannot use string literals as template parameters.
I acknowledge that macros are bad, however..
C++ lambdas have a special property of generating anonymous unique types.
Therefore a scope in which to create additional types.
This seems to be quite useful. It seems like producing types (or functions) within lambdas within macros provides template-like powers, and not in the bad old way that macros on their own do.
I'm not sure where all this will go, but here is a start:
#include <stdio.h>
#include <string>
#define STRING_TEMPLATE_FUNCTION(str) \
[]() { struct Type { static const char* Function() { return str; } }; return Type::Function; }()
#define STRING_TEMPLATE_CLASS(str) \
[=]() { struct Type : std::string { using std::string::operator=; Type() : std::string(str) { } }; return []{ return Type();}(); }()
int main()
{
auto foo = STRING_TEMPLATE_FUNCTION("foo");
auto bar = STRING_TEMPLATE_CLASS("bar");
auto bar2 = STRING_TEMPLATE_CLASS("bar2");
printf("%s\n", foo());
printf("%s\n", bar.c_str());
printf("%s\n", bar2.c_str());
bar = bar2;
printf("%s\n", bar.c_str());
}
No comments:
Post a Comment