I have the following code
struct R {
int i_ = 8;
R() = default;
R(R const& o) = default;
R(R&& o) = default;
R(int i) : i_(i) {}
operator int() const { return i_; }
};
struct S {
R i_;
operator R() const { return i_; }
operator int() const { return static_cast<int>(i_); }
};
int main() {
S s;
R r0(s);
R r = static_cast<R>(s);
float af[] = {1,2,3};
float f1 = af[s];
float f2 = af[r];
}
which compiles fine on Clang and GCC for C++17 and C++20 (not for C++ < 17) but complains about
'R::R': ambiguous call to overloaded function
note: could be 'R::R(R &&)'
note: or 'R::R(int)'
in MSVC for all available standards.
I tried to find a difference in the language conformances by comparing MSVC, Clang and GCC and also tried /permissive- for MSVC but couldn't find any explanation yet.
- Who is right (any why) and
- how can I make it compile for MSVC without giving up on the
R&&andint ictors in R or marking the castoperator int()s explicit?
Here's a rather crowded godbolt including a possible workaround.