Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88366

Re: g++ -c -Werror -Weverything exercise_2_17.cpp

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: g++ -c -Werror -Weverything exercise_2_17.cpp
Date 2023-01-03 08:51 +0100
Organization A noiseless patient Spider
Message-ID <tp0mqs$22vmh$1@dont-email.me> (permalink)
References (3 earlier) <tonjq0$ihs$1@gioia.aioe.org> <tonlf4$p0hj$1@dont-email.me> <topj40$hak$1@gioia.aioe.org> <topl5s$11pd6$2@dont-email.me> <tp0hrr$iih$2@gioia.aioe.org>

Show all headers | View raw


On 03/01/2023 07:27, Juha Nieminen wrote:
> David Brown <david.brown@hesbynett.no> wrote:
>>> One warning, or was it actually an error, that does annoy me quite a bit
>>> is the "narrowing conversion" thingie. I think it was introduced in C++11
>>> or newer, and was never a problem before. To this day I'm not sure why
>>> that warning/error is necessary.
>>>
>>> (Maybe it has something to do with uniform initialization, which turned
>>> out to be quite a mess?)
>>
>> I think warnings (or errors) about narrowing conversions are a good
>> idea.  If I accidentally try to use a double as an integer, I'd rather
>> be told.
> 
> The problem is that the "narrowing" warning/error is only issued when
> eg. using a double within an initializer list that expects a float,
> it doesn't happen in *any* other situation where a double is being
> implicitly converted into a float.
> 
> I cannot comprehend the reason to make that one particular situation
> special.

It is better than nothing.

But I agree with you that narrowing conversions can definitely be wrong, 
or at least risky, in other circumstances too.  /Increasing/ the type 
size silently can also be an issue (imagine using a microcontroller with 
hardware single-precision floating point, but doubles in software - 
these are very common).

C++ inherited a range of automatic conversions and promotions from C. 
They are often convenient, but occasionally problematic.  They can't be 
removed from the language - backwards compatibility is too strong for 
that.  It would usually not make sense to have compiler warning options 
either, as the false positives would likely swamp the useful warnings.

As I see it, there are three options.  Switch to a stricter language, 
such as Ada.  Make your own types, such as "Float" and "Double", without 
automatic conversions.  Or campaign for a new feature in C++ where a 
declaration like "void foo(explicit float f)" would require a float 
argument without conversion.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

g++  -c -Werror -Weverything exercise_2_17.cpp "gdo...@gmail.com" <gdotone@gmail.com> - 2022-12-27 01:22 -0800
  Re: g++ -c -Werror -Weverything exercise_2_17.cpp Öö Tiib <ootiib@hot.ee> - 2022-12-27 02:50 -0800
  Re: g++  -c -Werror -Weverything exercise_2_17.cpp Juha Nieminen <nospam@thanks.invalid> - 2022-12-29 20:33 +0000
    Re: g++ -c -Werror -Weverything exercise_2_17.cpp Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-29 23:20 +0200
      Re: g++ -c -Werror -Weverything exercise_2_17.cpp Juha Nieminen <nospam@thanks.invalid> - 2022-12-30 21:05 +0000
        Re: g++ -c -Werror -Weverything exercise_2_17.cpp Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-30 23:33 +0200
          Re: g++ -c -Werror -Weverything exercise_2_17.cpp Juha Nieminen <nospam@thanks.invalid> - 2022-12-31 15:05 +0000
            Re: g++ -c -Werror -Weverything exercise_2_17.cpp David Brown <david.brown@hesbynett.no> - 2022-12-31 16:40 +0100
              Re: g++ -c -Werror -Weverything exercise_2_17.cpp Juha Nieminen <nospam@thanks.invalid> - 2023-01-03 06:27 +0000
                Re: g++ -c -Werror -Weverything exercise_2_17.cpp David Brown <david.brown@hesbynett.no> - 2023-01-03 08:51 +0100
                Re: g++ -c -Werror -Weverything exercise_2_17.cpp Juha Nieminen <nospam@thanks.invalid> - 2023-01-03 09:13 +0000
                Re: g++ -c -Werror -Weverything exercise_2_17.cpp David Brown <david.brown@hesbynett.no> - 2023-01-03 11:47 +0100
            Re: g++ -c -Werror -Weverything exercise_2_17.cpp Paavo Helde <eesnimi@osa.pri.ee> - 2022-12-31 18:50 +0200
              Re: g++ -c -Werror -Weverything exercise_2_17.cpp David Brown <david.brown@hesbynett.no> - 2022-12-31 18:47 +0100

csiph-web