ABC30 NEWS

     
Recently when writing code I encountered a problem. The code was something lượt thích this-

if ((var >= min) && (var Both min and max are macros already defined previously and var is an unsigned integer. I changed the value of min to lớn 0, which generated a warning, unsigned integer >= 0 will always be true. First I thought khổng lồ directly remove the first condition to take care of the warning, but my senior refused to vì chưng that, as the code is used in not only the project I am working but also other projects, so maybe the value of min may be above 0 in other projects. He suggested me to it lượt thích this-

#define min (0-0)This solved the problem, no warning generated. But I did not understand why did it work? I asked my senior lớn explain but he said to lớn find out on my own. I searched on google but did not find anything remotely familiar khổng lồ it.So can anyone explain lớn me what happened above?Thank you in advance.

Bạn đang xem: Abc30 news


cốt truyện
Improve this question
Follow
asked May 20, 2020 at 7:37
*

ankit bansalankit bansal
2144 bronze badges
6
| Show 1 more bình luận

4 Answers 4


Sorted by: Reset to default
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
1
Answer: Because it seems the compiler doesn`t kiểm tra the result of expressions.

Just to know: This is a problem. The compiler is correct - unsigned int can not be less than 0. Whatever you pass lớn this function, that comparison will always be true.Because you are on work, bởi vì what the others tell you, but keep in mind - you are right, that should be removed, because it makes no sense in your current implementation.

Var has lớn be changed khổng lồ long long và then everything will be fine! The problem is there!It is a matter of time, someone to pass negative value khổng lồ that function và will fail hard. It will make a warning for passing signed value to a function expecting unsigned and the guy will deprecate it to lớn not have warnings ...


mô tả
Improve this answer
Follow
edited May 20, 2020 at 8:16
answered May 20, 2020 at 8:07
*

AlexAlex
9477 bronze badges
4
add a comment |
1
#define min (0-0) và #define min (0) would be equivalent for type và value point of view.

It seems that it allows lớn break the check of your compiler.

Xem thêm: Nam Kim Ngưu Hợp Với Cung Nào & Những Điều Cần Biết, Đàn Ông Cung Kim Ngưu Hợp Với Cung Nào

Real fix khổng lồ remove the warning is to lớn handle the case, for example:

if constexpr (min == 0 && std::is_unsigned_v>) { if (var = min && var which is probably too verbose (and even worse if you vày similar stuff for max).That cure seems not better than the warning :-/

Possible workaround would be khổng lồ wrap the test in a function,as I don"t think, currently, compilers warn about function returning always true in condition.

so:

// indirect comparison lớn avoid warning about always true comparisonif (std::less_equal(min, var) && std::less_equal(var, max)) return true;
nói qua
Improve this answer
Follow
answered May 20, 2020 at 9:47

*

Jarod42Jarod42
196k1313 gold badges178178 silver badges286286 bronze badges
2
add a comment |
0
A macro applies a tìm kiếm and replace before compiling:

So for #define min (0) the code for the compiler will be:

if ((var >= (0)) && (var & for #define min (0-0) it will be:

if ((var >= (0-0)) && (var Both var >= 0 & var >= 0-0 are equivalent regarding the types involved in the comparsion.

clang will for example correctly report both with the warning warning: result of comparison of unsigned expression >= 0 is always true <-Wtautological-unsigned-zero-compare>.

gcc reports only the (0) case.

Using 0-0 is a bad idea, not only because silencing a warning instead of solving it is a bad idea, but also because that gian lận might fail in future compiler versions. It just does not solve the problem, but just tricks the compiler. Và using macros to define constant also a bad idea.

By usning a constexpr for the constant instead of a macro the case of a >= 0 could have been correctly solved.

Xem thêm: Sơ Đồ Điện Máy Lọc Nước & Nguyên Lý Mạch Điện Của Máy Lọc Nước Ro

Removing var >= min is not necessarily a good idea if the "constant" min exists, as someone could change its value the future. Var >= min can only be safely removed the code should not rely on min & should never have a lower bound that is different lớn the one given by the type of val. So if min has any relevance then the code should be changed khổng lồ handle the case when it is 0 correctly, so that the compiler is aware that you know that this case could exist.