I would like to know a smart way to avoid repeating myself in the following declaration of similar structs. All the structs in the code below inherit publicly from boost::spirit::qi::symbols, where EnumType is different for each struct.
struct website_ : qi::symbols<char, Website>
{
website_() : qi::symbols<char, Website>(website_names, Website_values) { }
} website_;
struct currency_name_ : qi::symbols<char, Currency>
{
currency_name_ () : qi::symbols<char, Currency>(currency_names, Currency_values) { }
} currency_name_;
Where, for example
enum Website { /*list of websites*/ };
const std::vector<Website> Website_values = { /*same list as above*/ };
const std::vector<std::string> website_names = { /* list of corresponding names of websites*/ };
The goal is to initialize every struct giving only the appropriate enum, vector of enum values and vector of strings with the names of the values.
I have thought of a solution with a new subclass of qi::symbols, from which all of my structs would inherit but I think it might be an overkill, and another perhaps using macros (which I am not very familiar with). Is there another method using some other kind of metaprogramming? Or perhaps some other trick from the Boost Library, which I am already using anyway?
That’s why templates were invented.