了解visual studio 2010中编译器选项的差异和严格的C++兼容性
Understanding compiler options differences in visual studio 2010 and strict C++ compliance
我正在给一些作业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)
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 提升序列化 1:73 的向后兼容性问题
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- 各种版本的 Mac OSX 向后兼容性如何?(Xcode C++
- Visual Studio 19-17 库兼容性根据 GL 标志
- C++ ABI 兼容性问题/张量流
- Oracle OCCI 和 g++ 7.1 的兼容性问题
- 为什么库API+编译器ABI足以确保具有不同版本gcc的对象之间的兼容性
- 对指针和恒常性兼容性的引用
- 检查操作系统兼容性
- 添加noexcept是否会破坏二进制兼容性
- 右值需要分配什么向后兼容性
- 如何在不添加对库的依赖的情况下添加与库结构的兼容性
- 在Qt中使用C++Lambda函数作为插槽是否有助于保持库的二进制兼容性?
- python 和 swig 版本兼容性问题
- Tensorflow C++ API 和 Caffe 兼容性问题与 Glog 相关
- 当要求兼容性时,核心OpenGL上下文?
- 启用和禁用 RTTI 的模块之间的兼容性
- 值为 std::string,同时保持向后兼容性
- 将 OpenGL 兼容性配置文件与英特尔 HD GPU + Mesa 结合使用