I'm working with a large code base which uses const strings in structure initializers. I'm trying to translate these strings via GNU gettext with a minimal amount of time. Is there some sort of conversion operator I can add to default_value which will allow Case #1 to work?
#include <cstring>
template<int N> struct fixed_string
{
char text[N];
};
// Case #1
struct data1
{
char string[50];
};
// Case #2
struct data2
{
const char* string;
};
// Case #3
struct data3
{
fixed_string<50> string;
};
// A conversion helper
struct default_value
{
const char* text;
default_value(const char* t): text(t) {}
operator const char*() const
{
return text;
}
template<int M> operator fixed_string<M>() const
{
fixed_string<M> ret;
std::strncpy(ret.text, text, M);
ret.text[M - 1] = 0;
return ret;
}
};
// The translation function
const char* translate(const char* text) {return "TheTranslation";}
int main()
{
data1 d1 = {default_value(translate("Hello"))}; // Broken
data2 d2 = {default_value(translate("Hello"))}; // Works
data3 d3 = {default_value(translate("Hello"))}; // Works
}