Conversion warnings for std::copy and std::vector::assign

后端 未结 1 965
我在风中等你
我在风中等你 2021-02-19 18:50

When a floating point number is inserted into a std::vector, the number must be converted by some kind of rounding. Typically this changes the number, 1.

相关标签:
1条回答
  • 2021-02-19 19:32

    You can use the following flags -std=c++11 -Wfloat-conversion -Wsystem-headers to make GCC additionally at least warn for this LOC as well

    std::copy(vsource.begin(), vsource.end(), vtarget3.begin());
    

    as well as you can see here.

    The output is then

    <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
    

    As for clang: This flag set is rather verbose and not really giving more insight.

    0 讨论(0)
提交回复
热议问题