I have a struct like this:
struct VrtxPros{
long idx;
std::vector<std::string> pros;
VrtxPros(const long& _idx=-1, const std::string& val="") : idx(_idx)
{
if ( !val.empty() && val!="" )
pros.push_back(val);
}
};
and later in the code I use it like that:
long idx = 1234;
VrtxPros vp( 2134, std::string("-1") );
if ( margin ) vp.pros[0] = idx;
The compiler has no problem with that. I am wondering because the operator should deliver a reference.
I could not find an operator= in std::string which would accept a long as source.
Why does the code compile?
A
std::stringcan be assigned to achar, and alongcan be implicitly converted to achar, so astd::stringcan be assigned to along. Your compiler will probably give a warning about this kind of implicit conversion (turn up the warning level and you’ll see it, if you don’t already).See the #4
operator=listed here. Notice no constructor overload takes just a char, so this sort of thing can only be done for assignment.For that matter, you can do this too: