简体   繁体   English

std :: vector(或其他位置)中最奇怪的错误(或其他内容)。 这是怎么回事?

[英]The weirdest bug (or something) in std::vector (or somewhere else). What's going on?

I've tried compiling this with Visual Studio 2012 RC and Intel C++ Compiler XE 12.1. 我尝试使用Visual Studio 2012 RC和Intel C ++ Compiler XE 12.1进行编译。 I'd appreciate if you tried with some other compiler. 如果您尝试使用其他编译器,将不胜感激。 See my comments in the code to really appreciate the weirdness of this bug. 请参阅代码中的注释,以真正欣赏此错误的怪异之处。 Does anyone know what's going on, and where should I file a bug report about this? 有谁知道这是怎么回事,我应该在哪里提交有关此问题的错误报告?

// File: NamedSameA.h

#pragma once

// File: NamedSameA.cpp

#include <vector>

#include "NamedSameA.h"

struct NamedSame // Rename this class to something else to make the program work
{
    std::vector<int> data;
    // Comment out the previous line or change
    // the data type to int to make the program work
};

static NamedSame g_data; // Comment out this line to make the program work

// File: NamedSameB.h

#pragma once

void test();

// File: NamedSameB.cpp

#include <vector>

#include "NamedSameA.h"
#include "NamedSameB.h"

struct NamedSame
{
    int              data1; // Comment out this line to make the program work
    std::vector<int> data2;
};

void test()
{
    NamedSame namedSame;
    namedSame.data2.assign(100, 42);
    // The previous line produces the following runtime error:
    // -------------------------------------------------------
    // Debug Assertion Failed!
    // Program: C:\Windows\system32\MSVCP110D.dll
    // File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector
    // Line: 240
    // Expression: vector iterators incompatible
}

By giving the same name to two different classes/structures you've violated the One Definition Rule . 通过为两个不同的类/结构赋予相同的名称,您违反了“ 一个定义规则” This results in undefined behavior, so any result is possible - including a crash. 这将导致不确定的行为,因此任何结果都是可能的-包括崩溃。

I've found over the years that the more convinced I am that I've found a compiler bug, the more likely it is that my program has a fundamental flaw. 多年来,我发现我越相信我已经发现了编译器错误,就越有可能我的程序存在基本缺陷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM