gpt4 book ai didi

c++ - std::copy 和 std::vector::assign 的转换警告

转载 作者:IT老高 更新时间:2023-10-28 23:21:37 29 4
gpt4 key购买 nike

当 float 插入到 std::vector<int> 中时,数字必须通过某种舍入转换。通常这会更改数字,1.5 更改为 1 或 2,我希望编译器至少会警告此转换。所以我使用 -Wconversion在 g++ 或 clang++ 上标记。这将启用 std::vector::push_back 的警告或直接分配,但不适用于 std::copystd::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());

你可以看到here .

然后是输出

<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:这个标志集相当冗长,并没有真正提供更多洞察力。

关于c++ - std::copy 和 std::vector::assign 的转换警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52375445/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com