[英]Different behaviour between std deque/vector in MSVCC/g++/icc
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. 如果我在linux上用g ++和icc / icpc编译这个代码它编译得很好,即使用-Wall
它也没有警告。 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: 我想在Windows上使用MSVCC(cl)构建此代码但不幸的是它抛出错误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. 但是,如果我将std::deque
更改为std::vector
则可以使用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? 如果是这样,为什么编译器之间存在差异,或者这是g ++ / icc还是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. 它是未定义的行为(使用std::deque
和std::vector
,因此无论实现如何处理它都很好,就标准而言。你要实例化一个不完整类型的标准容器。
When compiling with g++, -Wall
(and in general, all options starting with -W
) only concern the language. 使用g ++编译时, -Wall
(通常,所有以-W
开头的选项)仅涉及语言。 For library issues, you should be compiling with -D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC
as well. 对于库问题,您应该使用-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC
进行编译。 (If this causes performance problems, you can remove the last two -D
in optimized builds.) (如果这会导致性能问题,则可以删除优化版本中的最后两个-D
。)
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. 继詹姆斯坎泽的回答之后,我四处寻找并找到了Dobbs博士的文章 ,该文章解释了标准在使用不完整类型的容器时的立场。
Further it hints at that the reason it works with vector
s but not deque
s, namely the implementation. 此外,它暗示了它适用于vector
而不是deque
的原因,即实现。 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. 因为我们只有一个指向T
的指针,但是deque
很可能以这种方式实现(在VS2010中),它使用T
值,从而使它与不完整的类型不兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.