Incomplete class usage in template

(I was waiting to Alf Steinbach to post an answer, but since he is not doing it, I will post the reference that he mentioned in the Lounge C++ chat): The standard indicates that template instantiations are performed after the translation unit has already been translated, so that in time, template instantiations happen after all … Read more

CRTP — accessing incomplete type members

When a class template is instantiated, its members other than non-virtual member functions are instantiated together with it. Non-virtual member functions, however, are only instantiated when odr-used (basically, called or have their address taken). When the compiler encounters class Implementation : public Interface<Implementation>, it needs to instantiate Interface<Implementation>. At this point, Implementation is still an … Read more

GCC: Array type has incomplete element type

It’s the array that’s causing trouble in: void print_graph(g_node graph_node[], double weight[][], int nodes); The second and subsequent dimensions must be given: void print_graph(g_node graph_node[], double weight[][32], int nodes); Or you can just give a pointer to pointer: void print_graph(g_node graph_node[], double **weight, int nodes); However, although they look similar, those are very different internally. … Read more

Does the GotW #101 “solution” actually solve anything?

You are correct; the example seems to be missing an explicit template instantiation. When I try to run the example with a constructor and destructor for widget::impl on MSVC 2010 SP1, I get a linker error for pimpl<widget::impl>::pimpl() and pimpl<widget::impl>::~pimpl(). When I add template class pimpl<widget::impl>;, it links fine. In other words, GotW #101 eliminates … Read more

Can standard container templates be instantiated with incomplete types?

Here’s my attempt at an interpretation: The standard simply says you mustn’t do this, even though any given concrete implementation may have no problem supporting such a construction. But imagine for example if someone wanted to write a “small vector” optimization by which a vector always contains space for, say, five elements. Immediately you’d be … Read more