Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading