I have a menu system that I want to initialise from constant data. A MenuItem can contain, as a sub-menu, a vector of MenuItems. But it only works up to a point. Here are the bare bones of the problem:
#include <vector>
struct S { std::vector<S> v ; } ;
S s1 = { } ;
S s2 = { { } } ;
S s3 = { { { } } } ;
g++ -std=c++0x (version 4.4.5) copes with s1 and s2, but s3 comes back with:
prog.cpp:6:22: error: template argument 1 is invalid
(see ideone). Am I doing something wrong?
GMan is correct in his comment: in the declaration of
S::vin your code,Sis still incomplete. A type must be complete to be usable as the value type in an STL container. For more information see Matt Austern’s article “The Standard Librarian: Containers of Incomplete Types.”If you were to switch to a container that is usable with an incomplete type, then your code is fine. For example, given the following:
then your original initialization should work fine:
This would work too: