c++initializationc++17variantstd-variant

Program with std::variant works in msvc but not in gcc


I wrote the following program that works with msvc c++17 but rejected by gcc and clang. I want to know which compiler is right here. Demo

#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
   
}

GCC says:

<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

Solution

  • I want to know which compiler is right here

    This is a confirmed msvc bug which should be applied as a DR to C++17. GCC and clang are right here.

    Here is the corresponding confirmed msvc bug report:

    MSVC accepts invalid std::variant b(1)