In my project there are a lot of strings with different meanings at the same scope, like:
std::string function_name = "name";
std::string hash = "0x123456";
std::string flag = "--configure";
I want to distinguish different strings by their meaning, to use with function overloads:
void Process(const std::string& string_type1);
void Process(const std::string& string_type2);
Obviously, I have to use different types:
void Process(const StringType1& string);
void Process(const StringType2& string);
But how to implement those types in an elegant manner? All I can come with is this:
class StringType1 {
std::string str_;
public:
explicit StringType1(const std::string& str) : str_(str) {}
std::string& toString() { return str_; }
};
// Same thing with StringType2, etc.
Can you advise more convenient way?
There is no point in renaming functions since the main goal is to not mistakenly pass one string type instead of another:
void ProcessType1(const std::string str);
void ProcessType2(const std::string str);
std::string str1, str2, str3;
// What should I pass where?..