Does anyone know why Dict
class is invalid but Dict2
is fine?
#include <string>
#include <unordered_map>
#include <map>
#include <variant>
class Dict
{
public:
Dict() {}
private:
std::unordered_map<std::string, std::variant<Dict, std::string>> data;
};
class Dict2
{
public:
Dict2() {}
private:
std::map<std::string, std::variant<Dict2, std::string>> data;
};
int main()
{
Dict d;
Dict2 d2;
return 0;
}
I get an error of
‘value’ is not a member of ‘std::is_trivially_move_constructible<Dict>’
.
I looked up the concept of trivially move and copy constructible, as I understood, the move contructor should be defined or deleted.
I am guessing it is because of using std::unordered_map
with std::variant
and the the compiler doesn't know how an object should be moved. But I am not sure if I have understood it correctly.
I believe the behavior is undefined in both cases.
Dict2
just happens to compile by accident of implementation; "seems to work" is one possible manifestation of undefined behavior.Dict
andDict2
are incomplete types until the closing brace of their definitions.std::variant
doesn't specifically allow being instantiated with an incomplete type.