This is my struct which uses implicit casting on creating variable.
#include <string>
#include <variant>
using namespace std;
using val = variant<int, string, double, bool, long, long long, long double>;
struct value
{
val innerVal;
value():innerVal(""){}
value(const val &c) : innerVal(c) {}
template <typename T>
operator T()
{
return get<T>(innerVal);
}
template <typename V>
value &operator=(const V &t)
{
innerVal = t;
return *this;
}
};
This is how I am using it when I construct a variable it works fine but when I assigned an already created variable to value struct it gives error.
int main(int argc, char* argv[])
{
value h;
h = "String";
string m = h;// Here works fine
string b = "different";
b = h;//Here it gives error
}
Compiler error
use of overloaded operator '=' is
ambiguous (with operand types
'std::__ndk1::string' (aka
'basic_string<char, char_traits<char>,
allocator<char> >') and 'value')
b = h;
I want to implicitly construct the variable without encountering this error can someone explain how to resolve this issue.
value h = "String";won't compile. See demostring m = h;there is no assignment, just a call to thestd::stringconstructor (the=is an initialisation, here, rather than an assignment). In the second case, you are actually using an assignment.value h = "String";but will acceptvalue h{ "String" };