1
votes

Today I came across a really strange behavior of the msvc2013 compiler. After nearly an hour, I found the causing bug. Minimal example:

#include <iostream>
#include <vector>


int main(int argc, char *argv[])
{
    float f1, f2;

    std::swap<float>(f1, f2);

    return 0;
}

When including std::vector, I get a bunch of very strange compiler errors with msvc2013! When compiling the code with mingw the code compiles fine, just as I would expect.

The errors:

main.cpp
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\xlocale(341): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1892): error C2825: '_Alloc': must be a class or namespace when followed by '::'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1944): note: see reference to class template instantiation 'std::_Vb_iter_base<_Alloc>' being compiled
        with
        [
            _Alloc=float
        ]
main.cpp(10): note: see reference to class template instantiation 'std::_Vb_reference<float>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1892): error C2510: '_Alloc': left of '::' must be a class/struct/union
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1892): error C2146: syntax error: missing '>' before identifier 'difference_type'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1898): error C2825: '_Alloc': must be a class or namespace when followed by '::'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1898): error C2510: '_Alloc': left of '::' must be a class/struct/union
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1898): error C3646: '_Sizet': unknown override specifier
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1898): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1906): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1913): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1920): error C2061: syntax error: identifier '_Sizet'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1937): error C3646: '_Myoff': unknown override specifier
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(1937): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

And yes - i do know this is now the msvc2015 compiler! I tested this one too, and these are the error messages from this compile

Is this a weird bug in msvc or why does and can this even happen?

1
We're sorry, but all of our psychics are out to lunch right now, and nobody in the office knows how to use the magical mind ray-beam machine, in order to extract the particulars of the "strange compiler errors" out of your head, so that everyone knows what they are, in order to figure out what they're all about. Thank you for your understanding.Sam Varshavchik
Include the compiler errors in your question.Matt
"Strange Behaviour"? You haven't assigned any values to f1 or f2, so how do you know something strange is happening? Are you sure its not because you are building in release mode and that's "upsetting" the debugger? It does what I expect, once I've fixed your code, in 2010 so you could look through the list of strange things fixed between the two versions. What do you think it should do?Code Gorilla
You should not be providing the template type but instead you should let template argument deduction work for you. swap should be used like std::swap(f1, f2);NathanOliver
i'm sorry! It's not about assigning values to f1 or f2 - it doesn't even compile when including <vector> ! If i dont include vector then everything works fine! The compiler errors are too long, i will include them in my original post!dumbperson

1 Answers

5
votes

This is a known issue in MSVC compilers. They argue that "calling swap() with explicit template arguments is not actually permitted by the Standard".