Thursday, October 13, 2022

Digest for comp.lang.c++@googlegroups.com - 18 updates in 3 topics

Ben Bacarisse <ben.usenet@bsb.me.uk>: Oct 13 12:19PM +0100

>> It seems that a lot of peculiar choices have been made in the code base.
 
> the example is only created to illustrate the copy propblem... its not
> an example from a real code.
 
Sure, but the loop running backwards, starting at size-1, introduces other
things to be careful about.
 
> The point is, that if I copy in a for-loop vector::size() to an
> integer, do you always do the casting for it?
 
No. Since the scope is small in a for loop, I'd change the declaration
and run the loop forward. It's more changes, but it will make the code
clearer.
 
If I had reliable regression tests, I'd consider writing
 
int sum = std::accumulate(v.begin(), v.end(), 0);
 
> Because there are many places that can happen....
 
Roughly how many? I think it makes sense to tidy up the code rather
than spray it with casts.
 
--
Ben.
JiiPee <kerrttuPoistaTama11@gmail.com>: Oct 13 08:27PM +0300

On 13/10/2022 12:00, Paavo Helde wrote:
> tests.
 
> When the code gets covered by tests enough, only then it becomes
> possible to make major code refactorings.
 
ye I agree, best way if possible to make tests to be sure changes will
not harm other code.
 
> understand that you want to get rid of warnings. It means you have to
> change all those hundreds of places anyway, so why not fix it properly
> instead of hiding the potential problems with a cast?
 
hmm, fair point. Especially if those places are not too many, like only
40 for example.
JiiPee <kerrttuPoistaTama11@gmail.com>: Oct 13 08:32PM +0300

On 13/10/2022 12:17, David Brown wrote:
> minimise noise.  Obviously the details here depend on the compiler and
> the warnings - as an example, you might have :
 
>     #pragma GCC diagnostic ignored "-Wsign-conversion"
 
good point, in old code can assume if its well tested.
Can I disable a warning blockwise or filewise in Visual Studio?
 
 
> Clearly this is a dangerous path - hiding the potential problems.  But
> maybe the code works fine in practice, as many compiler warnings are
> about /potential/ problems rather than real ones.
 
yes, if its for example tested 20 years....
 
> variables, refactorising, modernising, or otherwise changing the code.
> I'd be sceptical about simply adding static casts - the suggestion of
> using a function that supports optional run-time checks is probably better.
 
good point... rather use more sophisticated conversion function than cast.
JiiPee <kerrttuPoistaTama11@gmail.com>: Oct 13 08:35PM +0300

On 13/10/2022 10:52, Juha Nieminen wrote:
> So, in a manner of speaking, it's self-documenting code. Thus, I would
> use that.
 
I was thinking the same.
 
> you could use a function instead, and add a check in the function
> (for example an assert(), or possibly a throw), unless this is a
> very time-critical code (which I assume it isn't).
 
yes like other also said, function more accurate. So Currently I kinda
lean on the function solution.
Paavo Helde <eesnimi@osa.pri.ee>: Oct 13 09:13PM +0300

13.10.2022 20:32 JiiPee kirjutas:
> Can I disable a warning blockwise or filewise in Visual Studio?
 
MSVC also has pragmas to suppress warnings, with different syntax. Example:
 
#ifdef _MSC_VER
#pragma warning(disable:4100) // unreferenced formal parameter

No comments: