使用 std::variant 的程序可以在 msvc 中工作,但不能在 gcc 中工作

问题描述 投票:0回答:1

我编写了以下程序,该程序可与 msvc c++17 配合使用,但被 gcc 和 clang 拒绝。我想知道这里是哪个编译器。 演示

#include <variant>

struct C
{
    std::variant<bool> mem;
    C(std::variant<bool> p): mem(p)
    {

    }
};
int main()
{
    C c(1); //works with msvc but not with gcc and clang
   
}

海湾合作委员会说:

<source>: In function 'int main()':
<source>:13:10: error: no matching function for call to 'C::C(int)'
   13 |     C c(1); //works with msvc but not with gcc and clang
      |          ^
<source>:6:5: note: candidate: 'C::C(std::variant<bool>)'
    6 |     C(std::variant<bool> p): mem(p)
      |     ^
<source>:6:26: note:   no known conversion for argument 1 from 'int' to 'std::variant<bool>'
    6 |     C(std::variant<bool> p): mem(p)
      |       ~~~~~~~~~~~~~~~~~~~^
<source>:3:8: note: candidate: 'constexpr C::C(const C&)'
    3 | struct C
      |        ^
<source>:3:8: note:   no known conversion for argument 1 from 'int' to 'const C&'
<source>:3:8: note: candidate: 'constexpr C::C(C&&)'
<source>:3:8: note:   no known conversion for argument 1 from 'int' to 'C&&'
<source>:13:7: warning: unused variable 'c' [-Wunused-variable]
   13 |     C c(1); //works with msvc but not with gcc and clang
c++ c++17
1个回答
0
投票

我想知道这里是哪个编译器

GCC 和 clang 就在这里。这是 P0608R3 和一个已确认的 msvc bug,应作为 C++17 的 DR 应用。

这是相应的已确认的 msvc bug 报告:

MSVC 接受无效

std::variant b(1)

© www.soinside.com 2019 - 2024. All rights reserved.