std :: vector(或其他地方)中最奇怪的bug(或其他东西).这是怎么回事?

zer*_*s00 1 c++ bug-tracking vector stdvector

我尝试使用Visual Studio 2012 RC和英特尔C++编译器XE 12.1进行编译.如果您尝试使用其他编译器,我将不胜感激.在代码中查看我的评论,真正体会到这个bug的奇怪之处.有谁知道发生了什么,我应该在哪里提交有关此问题的错误报告?

// File: NamedSameA.h

#pragma once
Run Code Online (Sandbox Code Playgroud)
// 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
Run Code Online (Sandbox Code Playgroud)
// File: NamedSameB.h

#pragma once

void test();
Run Code Online (Sandbox Code Playgroud)
// 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
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*som 14

通过为两个不同的类/结构赋予相同的名称,您违反了" 一个定义规则".这会导致未定义的行为,因此任何结果都是可能的 - 包括崩溃.

多年来我发现我越相信我发现了编译器错误,我的程序就越有可能存在根本缺陷.