简体   繁体   English

MSVCC / g ++ / icc中std deque / vector之间的不同行为

[英]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::dequestd::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.

相关问题 std :: vector和std :: deque之间的大小差异 - Difference in size between std::vector and std::deque g++:编译一个巨大的静态 std::vector - g++: compiling a huge static std::vector g ++ -std = c ++ 11和Visual Studio 2013之间的变量地址不同 - Different variable address between g++ -std=c++11 and visual studio 2013 来自 g++ -fanalyzer 的 std::vector 空指针取消引用 - std::vector null pointer dereference from g++ -fanalyzer 在MacOSX上,使用g ++,std :: vector .size()线程安全吗? - On MacOSX, using g++, is std::vector .size() thread safe? g ++和clang ++使用运算符&lt;()重载的不同行为 - g++ and clang++ different behaviour with operator<() overloading g ++和clang ++使用指向可变参数模板函数的指针的不同行为 - g++ and clang++ different behaviour with pointer to variadic template functions g ++和clang ++使用自动参数的模板特化的不同行为 - g++ and clang++ different behaviour with template specialization for auto argument g++ 和 clang++ 与可变参数容器的不同行为 - g++ and clang++ different behaviour with variadic container 构造函数,初始化列表以及g ++和clang中的不同行为 - Constructor, initializer list and different behaviour in g++ and clang
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM