I have a class C that contains a struct S and a std::variant as members. The struct S has an int member a that is initialized to 0. Here is the code:
#include <variant>
class C
{
struct S {
int a = 0;
};
std::variant<S> test;
};
int main()
{
C ctest;
return 0;
}
When I try to compile this code with gcc 12.2.1 (also with different compilers), I get the following error:
main.cpp: In function 'int main(':
main.cpp:14:7: error: use of deleted function 'C::C()'
14 | C ctest;
| ^~~~~
main.cpp:3:7: note: 'C::C()' is implicitly deleted because the default definition would be ill-formed:
3 | class C
| ^
main.cpp:3:7: error: use of deleted function 'std::variant<_Types>::variant() [with _Types = {C::S}]'
In file included from main.cpp:1:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/variant:1402:7: note: 'std::variant<_Types>::variant() [with _Types = {C::S}]' is implicitly deleted because the default definition would be ill-formed:
1402 | variant() = default;
| ^~~~~~~
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/variant:1402:7: error: use of deleted function 'constexpr std::_Enable_default_constructor<false, _Tag>::_Enable_default_constructor() [with _Tag = std::variant<C::S>]'
In file included from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/variant:38:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/enable_special_members.h:113:15: note: declared here
113 | constexpr _Enable_default_constructor() noexcept = delete;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
You can see the code and the error message here: https://onlinegdb.com/ZdfGp9avn
However, if I remove the default assignment =0 from the struct S, the code compiles without errors. Why is this happening? How can I fix this error without removing the default assignment? What is the difference between having a default assignment or not in this case?
std::variant<S> test = S{};
is one option Demo. – Koehler