I have the following class:
class StackStringHolder
{
public:
StackStringHolder& operator=(const std::string& str)
{
str_ = str;
return *this;
}
StackStringHolder& operator=(std::string&& str)
{
str_ = std::move(str);
return *this;
}
const std::string& get() const { return str_; }
private:
std::string str_;
};
And I want to use it in the std::variant the following way:
int main() {
std::variant<int, StackStringHolder> var;
var = 5;
std::string str = "Hello";
var = str; // error
var = std::move(str); // error
}
However compiler (MSVS2022) says:
error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Update, I'm sure my pieces of code enough, but as I'm asked for, here is the complete example:
#include <string>
#include <variant>
class StackStringHolder
{
public:
StackStringHolder& operator=(const std::string& str)
{
str_ = str;
return *this;
}
StackStringHolder& operator=(std::string&& str)
{
str_ = std::move(str);
return *this;
}
const std::string& get() const { return str_; }
private:
std::string str_;
};
int main() {
std::variant<int, StackStringHolder> var;
var = 5;
std::string str = "Hello";
var = str; // error
var = std::move(str); // error
return 0;
}