STL Question
-
Hello there, is it possible to instantiate a std::vector with an incomplete type? E.g. --Header-- struct field_t; class all_t { ... private: std::vector m_fields; }; --Implementation-- struct field_t { long val1; int val2; }; all_t::all_t() { ... m_fields.resize(10); ... } --End-- What size is assumed by the vector when resize(10) is called? It seems to work with VC7.1 but is it portable or will it overwrite the heap sometimes later? Regards Josef
-
Hello there, is it possible to instantiate a std::vector with an incomplete type? E.g. --Header-- struct field_t; class all_t { ... private: std::vector m_fields; }; --Implementation-- struct field_t { long val1; int val2; }; all_t::all_t() { ... m_fields.resize(10); ... } --End-- What size is assumed by the vector when resize(10) is called? It seems to work with VC7.1 but is it portable or will it overwrite the heap sometimes later? Regards Josef
Josef Schroettle wrote: is it possible to instantiate a std::vector with an incomplete type? E.g. Oddly, enough, yes. As long as field_t is known at the time of instantiating variables of type all_t. It seems that any type which embeds a template instantiation doesn't really get instantiated until it's used to declare a value variable (i.e., not a pointer or reference). To be honest, I'm not sure if this is stipulated by the C++ standard or if this is a Microsoft extension. Josef Schroettle wrote: or will it overwrite the heap sometimes later? I doubt it. MSVC doesn't allow you to instantiate all_t unless field_t is known at the point of instantiation. Since field_t is known, then also its size is known, and therefore the heap will be just fine. If you do find whether this is standard C++ or not, please let me know. -- Din mamma.
-
Josef Schroettle wrote: is it possible to instantiate a std::vector with an incomplete type? E.g. Oddly, enough, yes. As long as field_t is known at the time of instantiating variables of type all_t. It seems that any type which embeds a template instantiation doesn't really get instantiated until it's used to declare a value variable (i.e., not a pointer or reference). To be honest, I'm not sure if this is stipulated by the C++ standard or if this is a Microsoft extension. Josef Schroettle wrote: or will it overwrite the heap sometimes later? I doubt it. MSVC doesn't allow you to instantiate all_t unless field_t is known at the point of instantiation. Since field_t is known, then also its size is known, and therefore the heap will be just fine. If you do find whether this is standard C++ or not, please let me know. -- Din mamma.
Jörgen Sigvardsson wrote: I doubt it. MSVC doesn't allow you to instantiate all_t unless field_t is known at the point of instantiation. Since field_t is known, then also its size is known, and therefore the heap will be just fine The question is there, that clients which just include the header file don't know the size of 'T'. But I assume that the vector has always the same size and sizeof(T) is just needed when vector gets memory for a chunk of T's. But in my opinion this is highly depending on the STL implementation, one could e.g. cache the sizeof(T) in a local variable of vector<> which would then be not the real size which is only known to the implementation file. I'll try asking Scott Meyers which wrote the excellent book 'Effective STL'. Josef
-
Jörgen Sigvardsson wrote: I doubt it. MSVC doesn't allow you to instantiate all_t unless field_t is known at the point of instantiation. Since field_t is known, then also its size is known, and therefore the heap will be just fine The question is there, that clients which just include the header file don't know the size of 'T'. But I assume that the vector has always the same size and sizeof(T) is just needed when vector gets memory for a chunk of T's. But in my opinion this is highly depending on the STL implementation, one could e.g. cache the sizeof(T) in a local variable of vector<> which would then be not the real size which is only known to the implementation file. I'll try asking Scott Meyers which wrote the excellent book 'Effective STL'. Josef
Josef Schroettle wrote: that clients which just include the header file don't know the size of 'T'. it doesn't matter, because the compiler will not try to calculate the sizes of container<T> until it really has to. Like in a .cpp file. And when that happens, the compiler will remind you that T is unknown if the declaration of T is not visible in the scope you're instantiating container<T>. -- Din mamma.
-
Josef Schroettle wrote: that clients which just include the header file don't know the size of 'T'. it doesn't matter, because the compiler will not try to calculate the sizes of container<T> until it really has to. Like in a .cpp file. And when that happens, the compiler will remind you that T is unknown if the declaration of T is not visible in the scope you're instantiating container<T>. -- Din mamma.
Hello Jörgen, Scott Meyers answered very fast and I include his reponse below: -- Scott Meyers wrote --- Not in portable code. Section 17.4.3.6 of the Standard states that using an incomplete type to instantiate a library component (such as vector) yields undefined behavior. On this topic, I encourage you to read http://www.cuj.com/documents/s=7986/cujcexp2002austern/ -- End Scott Meyers --- Sometimes it's good to get an authoritative answer from an expert... Josef
-
Hello Jörgen, Scott Meyers answered very fast and I include his reponse below: -- Scott Meyers wrote --- Not in portable code. Section 17.4.3.6 of the Standard states that using an incomplete type to instantiate a library component (such as vector) yields undefined behavior. On this topic, I encourage you to read http://www.cuj.com/documents/s=7986/cujcexp2002austern/ -- End Scott Meyers --- Sometimes it's good to get an authoritative answer from an expert... Josef
Josef Schroettle wrote: _On this topic, I encourage you to read http://www.cuj.com/documents/s=7986/cujcexp2002austern/_ Clickety[^]