[英]How to disable Visual Studio warning C4244 for std::vector copy or assign with implicit narrowing conversion
[英]Conversion warnings for std::copy and std::vector::assign
當浮點數插入到std::vector<int>
,必須通過某種舍入來轉換該數字。 通常這會更改數字,1.5會更改為1或2,我希望編譯器至少會警告此轉換。 所以我在g ++或clang ++上使用-Wconversion
標志。 這將啟用std::vector::push_back
或直接賦值的警告,但不能用於std::copy
或std::vector::assign(iterator first, iterator end)
。
現在我的問題是:如何獲得std::copy
和std::vector::assign
轉換警告?
這是我的示例程序:
#include <iostream>
#include <vector>
#include <algorithm>
using source_type = std::vector<double>;
using target_type = std::vector<int>;
int main() {
source_type vsource;
target_type vtarget1;
target_type vtarget2;
target_type vtarget3;
target_type vtarget4;
// Fill source with a number
vsource.push_back(1.5);
// This will give a compiler warning as expected
vtarget1.push_back(vsource.at(0));
// This does not give a warning, why not?
vtarget2.assign(vsource.begin(), vsource.end());
// Also this does not give a warning, why not?
vtarget3.assign(vsource.size(), 0);
std::copy(vsource.begin(), vsource.end(), vtarget3.begin());
// The following should be equivalent to std::copy according to
// http://www.cplusplus.com/reference/algorithm/copy/?kw=copy
// Here we get a warning as expected (in contrast to std::copy).
vtarget4.assign(vsource.size(), 0);
auto source = vsource.begin();
auto target = vtarget4.begin();
while (source != vsource.end()) {
*target = *source;
++target; ++source;
}
std::cout << vsource.at(0) << " "
<< vtarget1.at(0) << " "
<< vtarget2.at(0) << " "
<< vtarget3.at(0) << " "
<< vtarget4.at(0) << std::endl;
return 0;
}
我編譯:
g++ -Wall -Wextra -Wconversion -std=c++11 -pedantic
clang++ -Wall -Wextra -Wconversion -std=c++11 -pedantic
我只收到兩個警告,我想再多說一些:
question.cpp:22:24: warning: implicit conversion turns floating-point number into integer: 'value_type' (aka 'double')
to 'value_type' (aka 'int') [-Wfloat-conversion]
vtarget1.push_back(vsource.at(0));
question.cpp:40:18: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
[-Wfloat-conversion]
*target = *source;
您可以使用以下標志-std=c++11 -Wfloat-conversion -Wsystem-headers
使GCC另外至少警告此LOC
std::copy(vsource.begin(), vsource.end(), vtarget3.begin());
你也可以看到這里 。
然后輸出
<source>: In function 'int main()':
<source>:21:34: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<double>, double>::value_type' {aka 'double'} to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wfloat-conversion]
21 | vtarget1.push_back(vsource.at(0));
| ~~~~~~~~~~^~~
<source>:38:18: warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion]
38 | *target = *source;
| ^~~~~~~
In file included from /opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/ios:40,
from /opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/ostream:38,
from /opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/bits/stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = double*; _OI = int*]':
/opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/bits/stl_algobase.h:400:30: required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = double*; _OI = int*]'
/opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/bits/stl_algobase.h:437:30: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<double*, std::vector<double> >; _OI = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]'
/opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/bits/stl_algobase.h:470:7: required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<double*, std::vector<double> >; _OI = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]'
<source>:28:63: required from here
/opt/compiler-explorer/gcc-trunk-20180917/include/c++/9.0.0/bits/stl_algobase.h:338:18: warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion]
338 | *__result = *__first;
| ^
Compiler returned: 0
至於clang:這個標志集相當冗長,並沒有給出更多的洞察力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.