I have a class that somewhat imitates the lazy evaluation seen in shell variables. It saves the value as a string, but when operators like +
are called, if both sides can be converted to int
, it will add them, if both can't be converted to int
, it treats it as a string concatenation, and if one can convert but the other can't, it prints an error message to handle the ambiguity.
The problem comes with the =
operator. I've already written the member function, so that's not an issue. What I would like to have happen in addition to the member function, is that when the lhs is an string and the rhs is my class, the operator should return my class's value as is, but if the lhs is an integer, it should return std::stoi(Value)
.
I've been reading up on templates, conditions, and std::enable_if
, so I think I'm on the right track, but I can't find any examples to wrap my head around how to accomplish this task.
In essence, what I want to do (although this code is illegal in C++) is the following:
MyClass operator = (std::string lhs, const MyClass rhs)
{
return MyClass.Value;
}
MyClass operator = (int lhs, const MyClass rhs)
{
return std::stoi(MyClass.Value);
}