Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dangerous ways of removing compiler warnings?

I like to force a policy of no warnings when I check someone's code. Any warnings that appear have to be explicitly documented as sometimes it's not easy to remove some warnings or might require too many cycles or memory etc.

But there is a down-side to this policy and that is removing warnings in ways that are potentially dangerous, i.e. the method used actually hides the problem rather than fixes it.

The one I'm most acutely aware of is explicitly casts which might hide a bug.

What other potentially dangerous ways of removing compiler warnings in C(++) are there that I should look out for?

like image 406
Makis Avatar asked Jan 21 '26 04:01

Makis


2 Answers

const correctness can cause a few problems for beginners:

// following should have been declared as f(const int & x)
void f( int & x ) {
  ...
}

later:

// n is only used to pass the parameter "4"
int n = 4;
// really wanted to say f(4)
f( n );

Edit1: In a somewhat similar vein, marking all member variables as mutable, because your code often changes them when const correctness says it really shouldn't.

Edit2: Another one I've come across (possibly from Java programmers ) is to tack throw() specifications onto functions, whether they could actually throw or not.

Well, there's the obvious way - disabling a specific warning for parts of the code:

#pragma warning( disable : 4507 34 )

EDIT: As has been pointed out in the comments, it is sometimes necessary to use in cases where you know that the warnings are OK (if it wasn't a useful feature, there would have been no reason to put it in in the first place). However, it is also a very easy way to "ignore" warnings in your code and still get it to compile silently, which is what the original question was about.

like image 45
Tal Pressman Avatar answered Jan 22 '26 17:01

Tal Pressman