I have this very simple piece of code;
#include <deque>
#include <vector>
using namespace std;
class A
{
public:
A(){};
~A(){};
deque<A> my_array; // vector<A> my_array;
};
int main(void)
{
}
If I compile this code with both g++ and icc/icpc on linux it compiles fine, even with -Wall
it gives no warnings. If I swap the deque to a vector the situation is the same.
I would like to build this code on windows using MSVCC (cl) but unfortunately it throws error c2027:
error C2027: use of undefined type 'A'
If however I change the std::deque
to a std::vector
it compiles fine with Visual Studio 2010.
My question is; is this behaviour to be expected for some reason? If so, why are there differences between compilers or is this a mistake with either g++/icc or MSVCC?
It's undefined behavior (both with std::deque
and with std::vector
, so whatever an implementation does with it is fine, as far as the standard is concerned. You're instantiating a standard container with an incomplete type.
When compiling with g++, -Wall
(and in general, all options starting with -W
) only concern the language. For library issues, you should be compiling with -D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC
as well. (If this causes performance problems, you can remove the last two -D
in optimized builds.)
Further to James Kanze's answer I searched around and found this Dr Dobbs article which explains the standard's stance on using containers with incomplete types.
Further it hints at that the reason it works with vector
s but not deque
s, namely the implementation. A typical vector may be something like
class vector<T> {
T* buff;
int size;
// ... snip
};
which is okay with imcomplete types since we have only a pointer to T
but deque
could well be implemented in such a way (in VS2010) that it makes use of T
by value thus rendering it incompatible with incomplete types.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.