使用 /permissive
编译成功,但使用 /permissive-
失败。哪些地方不符合规范,如何解决?
为什么在 (2)
中正常,而在 (4)
(3)
中失败?
如果删除 operator long
也会正常。
如何在不更改调用站点 (3,4)
的情况下解决这个问题?
#include <string>
struct my
{
std::string myVal;
my(std::string val): myVal(val) {}
operator std::string() { return myVal; };
operator long() { return std::stol(myVal); };
};
int main()
{
struct MyStruct
{
long n = my("1223"); // (1)
std::string s = my("ascas"); // (2)
} str;
str.s = my("ascas"); // (3)
str.n = my("1223"); // (4)
}
错误信息
error C2593: 'operator =' is ambiguous
xstring(2667): note: could be 'std::basic_string<...> &std::basic_string<...>::operator =(const _Elem)'
with
[
_Elem=char
]
xstring(2648): note: or 'std::basic_string<...> &std::basic_string<...>::operator =(const std::basic_string<...> &)'
xstring(2453): note: or 'std::basic_string<...> &std::basic_string<...>::operator =(std::basic_string<...> &&) noexcept(<expr>)'
Source1.cpp(17): note: while trying to match the argument list '(std::string, my)'
str.s = my("ascas").operator std::string();
(虽然我很少在实际应用中看到这种方法)。 - Eljay