mercredi 24 juillet 2019

Simplifying Static Const Member Definitions of Templated Class

I am trying to define a set of static const member variables of a class clearly, but I feel like my code is being overwhelmed by boilerplate syntax.

Here's an example:

template <typename InputT, typename OutputT>
class MyClassWithALongName {
    public:
        static const std::string kParameterOne;
        static const std::string kParameterTwo;
        static const std::string kParameterThree;
};

template <typename InputT, typename OutputT>
const std::string MyClassWithALongName<InputT, OutputT>::kParameterOne = "The quick brown fox";
template <typename InputT, typename OutputT>
const std::string MyClassWithALongName<InputT, OutputT>::kParameterTwo = " jumps over";
template <typename InputT, typename OutputT>
const std::string MyClassWithALongName<InputT, OutputT>::kParameterThree = " the lazy dog.";

The problem that I see is that the important information to the reader

const std::string MyClassWithALongName::kParameterOne = "The quick brown fox";
const std::string MyClassWithALongName::kParameterTwo = " jumps over";
const std::string MyClassWithALongName::kParameterThree = " the lazy dog.";

is being swallowed up by all the template syntax. When the names are shorter, putting everything on one line helps to visually group the boilerplate template syntax and allow the reader to automatically filter it out. However, with longer names that's not an option.

Is there a more readable way of defining these constants in this case that makes the important information more clear?

Bonus: is there some magic way to get rid of the templating altogether for these static const values? Obviously they don't actually depend on the template types at all.

Aucun commentaire:

Enregistrer un commentaire