HanXu
HanXu

Reputation: 5597

Incomplete types with Clang compiling c++11 code

As this website shows, following code will not be supported in Clang using C++11:

class Node {
    vertex<Node> children;
};

An error will occur:

field has incomplete type 'Node'

But such code is supported in C++98 and other compilers such as gcc in C++11.

I know I can use

vertex<Node*>

instead, but at present I have some incompatibility issue with old code in C++98.

My question is, (1) can I compile such code using Clang in C++11? (2) I think a tree structure does inevitably need definition like above, without support of such feature, how can I realize such tree structure?


update:

Sorry for forgetting to give definition of vertex, What about the following code:

class Node {
    vector<Node> children;
};

Just change vertex into a container vector. It is not valid in Clang with C++11, but ok with other compilers and with C++98.


update again:

It seems vector works OK..but list fails

class Node {
    std::list<Node> children;
};

update again:

Following is my code:

#include <list>

using namespace std;

class Node {
    list<Node> nodes;
};

int main(int argc, char const *argv[])
{
    return 0;
}

or simpler:

#include <list>

class Node {
    std::list<Node> nodes;
};

int main() {}

I'm using Clang 4.0 and using the following command to compile:

clang++ -std=c++11 -stdlib=libc++ test.cpp

The error is

/usr/bin/../lib/c++/v1/list:212:9: error: field has incomplete type 'Node'

Upvotes: 0

Views: 1806

Answers (1)

Matthieu M.
Matthieu M.

Reputation: 300359

If it does not compile, it means that vertex attempts to use Node in a way that requires it to be completely defined. Most of the time, this implies (for generic code) using the size of the T parameter:

  • either explicitly (sizeof(T))
  • or implicitly template <typename T> struct vertex { T data[3]; }; is using the size of T to compute the layout of the type

Another (possible) issue, is relying on methods of T for some template instantiation; however this is much rarer.

You can avoid this requirement by changing the definition of vertex. Not knowing what it is though, we won't be able to get much more specific...

Upvotes: 3

Related Questions