Question

I've tried compiling this with Visual Studio 2012 RC and 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
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top