Can I create a template object A <B> containing an object of type A <B> objects?
I would like to understand if the following code is correct at all or not:
#include <deque> template<class T> struct Node { std::deque<Node<T> > branches; T data; Node(const T& _data) : data(_data) { } }; void dummy() { Node<float> test(5.0f); } This code has been compiled with several toolchains without error (see here ). The problem is that now I get an error creating the instance (possibly due to the fact that I'm using llvm lib ++, not sure about the version).
<...>llvm-libc++/include/deque:912:55: error: invalid application of 'sizeof' to incomplete type 'std::__1::__deque_base<Node<float>, std::__ 1::allocator<Node<float> > >::value_type {aka Node<float>}' static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16; In case this code is correct, I am not interested in investigating the origin of the error (implementation of the compiler or std library) or have a workaround: again, I would like to first of all understand if the code above is formally correct.
The code is poorly formed. At this stage:
template<class T> struct Node { std::deque<Node<T> > branches; // <== Node<T> is still an incomplete type. There are currently built-in exceptions for incomplete types for three containers:
- "An incomplete type
Tcan be used when instantiatingforward_listif the dispenser satisfies the distribution requirements for completeness 17.6.3.5.1.Tmust be complete before any member of the obtained specialization fromforward_list. [Forwardlist.overview] / 4 - "An incomplete type
Tcan be used when instantiating alistif the dispenser satisfies the completeness of the dispenser of requirement 17.6.3.5.1.Tmust be complete before any member of the resulting specializationlistis referenced." [list.overview] / 3 - "An incomplete type
Tcan be used when creating avectorinstance if the dispenser satisfies the dispenser completeness of requirement 17.6.3.5.1.Tmust be completed before any member of the receivedvectorspecialization is referenced." [Vector.overview] / 3
There is no wording for deque that currently requires the full type. It seems inappropriate for me to allow incomplete types for vector , but not deque , but the way it is.
This is not a bug in your compiler or standard library. The program is poorly formed. You cannot create an instance of std::deque with an incomplete value type.