vendredi 6 mars 2015

incomplete type for std::unordered_set compiling error in g++5, compiles in clang++

Consider the code related to a previous SO question C++ cyclic dependency confusion with adjacency list representation



#include <cstddef>
#include <unordered_set>

class Node;

class Hash {
public:
std::size_t operator()(const Node &node) const;
};

class Node {
public:
int data;
std::unordered_set<Node, Hash> links;
};

inline size_t Hash::operator()(const Node &node) const {
return node.data;
}

int main()
{

}


This code does not compile when using g++4.9.2 or g++5, however compiles with clang++3.5.


The error spit out by g++ starts with



error: invalid application of 'sizeof' to incomplete type 'Node' : std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>



Question: Does Node have to be a complete type when declaring an std::unordered_set? Looks like either g++ or clang++ is wrong in this case.


PS: I know this situation can be avoided by using a std::shared_ptr<Node> instead, however would like to understand the behaviour in the code above.


Aucun commentaire:

Enregistrer un commentaire