1

これを Visual Studio 2012 RC と Intel C++ Compiler XE 12.1 でコンパイルしてみました。他のコンパイラで試していただければ幸いです。このバグの奇妙さを本当に理解するには、コード内の私のコメントを参照してください。何が起こっているのか知っている人はいますか? また、これに関するバグ レポートはどこに提出すればよいですか?

// 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
}
4

1 に答える 1

14

2つの異なるクラス/構造に同じ名前を付けることにより、単一定義規則に違反しました。これにより、未定義の動作が発生するため、クラッシュを含むあらゆる結果が発生する可能性があります。

何年にもわたって、コンパイラのバグを見つけたと確信すればするほど、私のプログラムに根本的な欠陥がある可能性が高くなります。

于 2012-08-14T15:36:40.960 に答える