了解visual studio 2010中编译器选项的差异和严格的C++兼容性

Understanding compiler options differences in visual studio 2010 and strict C++ compliance

本文关键字:兼容性 C++ 了解 2010 studio 编译器 选项 visual      更新时间:2023-10-16

我正在给一些作业C++代码打分,一名学生使用了一个非标准的向量构造函数:

vector<vector<double> > A(rows, cols);

其中CCD_ 1和CCD_。我们在课堂上教它的方式是

vector<vector<double> > A(rows, vector<double>(cols));

遵循fill构造函数(中的2http://www.cplusplus.com/reference/vector/vector/vector/)

我正在使用一个批处理文件用命令行编译所有学生的代码

cl /O2 /EHsc /Tp <filename>

这个命令在上面提到的学生行抛出了这个错误:

error C2664: 'std::vector<std::vector<double,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>::vector(std::initializer_list<std::vector<_Ty,std::allocator<_Ty>>>,const std::allocator<std::vector<_Ty,std::allocator<_Ty>>> &)' : cannot convert argument 2 from 'unsigned int' to 'const std::vector<double,std::allocator<_Ty>> &'
    with
    [
        _Ty=double
    ]
    Reason: cannot convert from 'unsigned int' to 'const std::vector<double,std::allocator<_Ty>>'
    with
    [
        _Ty=double
    ]
    Constructor for class 'std::vector<double,std::allocator<_Ty>>' is declared 'explicit'
    with
    [
        _Ty=double
    ]

但是,当我创建一个项目并使用MSVC 2010的默认参数构建它时,它既不会发出警告,也不会出错。

我正在尝试了解什么编译器选项负责允许它在IDE中无警告地通过,以及我将如何关闭它

我试着在中找到答案http://msdn.microsoft.com/en-us/library/2tb15w2zaspx,但我无法回答。

编辑:我认为这可能对其他人有帮助:多亏了我现在理解的注释,IDE中调用的构造函数是范围构造函数(上面链接中的#3)。

这是我的特别问题:两个方法都使用相同的编译器,但有不同的选项(一个是IDE中的默认选项,另一个如上所述)。批处理文件抛出一个错误,IDE没有。我需要帮助确定IDE的命令行参数中要更改的内容,以便它抛出错误。

更新:我包含了错误消息。

更新2:原来脚本是在装有MSVC 2013的计算机上运行的,这就是的区别

实际上:

std::vector<std::vector<double> a( rows, columns );

是合法的,或者至少在C++11之前是合法的。事实并非如此故意合法,但标准的措辞方式编译器必须接受它。(我认为C++11解决了这个问题。尽管这样做有点太晚了,因为这样做会中断以前的法律法规。)

如果构造函数中的构造函数结果<template Iter>std::vector::vector( Iter begin, Iter end )被选择,并且Iter推导出积分类型必须表现得好像std::vector::vector( static_cast<size_type>( begin ), static_cast<value_type>( end ) )被调用。

在命令行编译时,您没有使用VS 2010。

以下是我从VS2010命令行编译中得到的内容:

C:so-test>cl /O2 /EHsc /Tp test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.
test.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.
/out:test.exe
test.obj

当我从VS2013命令行编译时,我收到了您的错误消息(我从VS2012命令行收到了一条类似但略有不同的消息)。

我建议您查看显示的版本号,以验证您在命令行中使用的MSVC编译器的版本。

  • VS 2010为16.00.xxxx(SP1为16.00.40219.01)
  • VS 2012为17.00.xxxx(更新4为17.00.61030)
  • VS 2013为18.00.xxxx(更新1为18.00.21005.1)