【发布时间】:2019-12-15 05:02:09
【问题描述】:
以下代码 compiles fine 带有 g++ 9.1 和 clang 8.0.0(编译标志是 -std=c++17 -Wall -Wextra -Werror -pedantic-errors),但不带有 MSVC 19.22(编译标志为/std:c++17 /permissive-):
struct X{};
struct Bar
{
Bar() = default;
Bar(X){}
};
struct Foo
{
operator X() const
{
return X{};
}
operator Bar() const
{
return Bar{};
}
};
int main()
{
Foo foo;
[[maybe_unused]]Bar b1 = foo; // OK
[[maybe_unused]]Bar b2(foo); // failed
}
MSVC 编译错误:
<source>(27): error C2668: 'Bar::Bar': ambiguous call to overloaded function
<source>(8): note: could be 'Bar::Bar(Bar &&)'
<source>(7): note: or 'Bar::Bar(X)'
<source>(27): note: while trying to match the argument list '(Foo)'
这是 MSVC 中的错误吗?
【问题讨论】:
-
在RexTester,我也收到了
note: or 'Bar::Bar(const Bar &)',这更有意义。 -
@PaulSanders Rextester 使用旧版本的 MSVC(与 Microsoft Visual Studio 2015 一起提供),在我使用过 MSVC 19.22(与最新的 MSVS 2019 版本 16.2 一起提供)可用。
标签: c++ initialization language-lawyer c++17 conversion-operator