使用带模板参数的swap与std :: vector包含

时间:2016-06-10 12:27:55

标签: c++ visual-c++ compiler-errors swap

今天我遇到了msvc2013编译器的一个非常奇怪的行为。 差不多一个小时后,我发现了导致错误的原因。 最小的例子:

#include <iostream>
#include <vector>


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

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

    return 0;
}

当包含std :: vector时,我在msvc2013中遇到了一堆非常奇怪的编译器错误!使用mingw编译代码时,代码编译正常,就像我期望的那样。

错误:

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

是的 - 我知道这是现在的msvc2015编译器!我也测试了这个,这些是来自这个编译的错误消息

这是msvc中的一个奇怪的错误,为什么会这样甚至发生?

1 个答案:

答案 0 :(得分:5)

这是MSVC编译器中的known issue。他们认为“标准”实际上不允许“使用显式模板参数调用swap()”。