你怎么能emplace_back错误的类型?
How can you emplace_back the wrong type?
我有一个双精度向量。但我有一个错字
我打算写这个:
std::vector<double> timestamp;
但我写了这个:
std::vector<std::vector<double>> timestamp;
但是,这会编译
timestamp.emplace_back(a_double_timestamp)
我正在将double
放回std::vector<std::vector<double>>
.double
不是std::vector<double>
>double
被隐式转换为size_type
,作为向量构造函数的参数:
explicit vector( size_type count );
因此,如果传递2.3
,则创建的向量的大小为static_cast<std::vector<double>::size_type>(2.3) == 2
。
std::vector<double> v{2.3}
也会在没有任何警告的情况下进行编译。
你收到任何警告吗?MSVC 发出警告
">参数":从"双精度"转换为"常量无符号__int64", 可能丢失的数据
虽然海湾合作委员会头保持沉默。
这里发生的情况是调用内部向量explicit vector( size_type count );
构造函数,自动从双精度转换为size_type
我逐步浏览了代码以查看发生了什么,我看到的是(无论如何在Visual Studio 2017中(正在调用以下构造函数:
vector(_CRT_GUARDOVERFLOW const size_type _Count, const _Alloc& _Al = _Alloc())
。这似乎暗示双精度被隐式转换为size_type(我在测试中使用了 12.3 的双精度,调试器告诉我_Count是 12(。_Al
具有默认值,因此构造函数匹配。
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误