if ((var >= min) && (var Both min and max are macros already defined previously và var is an unsigned integer. I changed the value of min khổng lồ 0, which generated a warning, unsigned integer >= 0 will always be true. First I thought khổng lồ directly remove the first condition to lớn 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 to explain but he said khổng lồ find out on my own. I searched on google but did not find anything remotely familiar to it.So can anyone explain lớn me what happened above?Thank you in advance.
Bạn đang xem: 0
c++ macros
nói qua
Follow
asked May 20, 2020 at 7:37

ankit bansalankit bansal
1944 bronze badges
6
| Show 1 more phản hồi
4 Answers 4
Sorted by: Reset to default
Trending sort available
Trending sort Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to lớn sorting by highest score if no posts are trending.
Switch to Trending sortHighest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
1
Answer: Because it seems the compiler doesn`t check the result of expressions.
Just to lớn know: This is a problem. The compiler is correct - unsigned int can not be less than 0. Whatever you pass to this function, that comparison will always be true.Because you are on work, bởi 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 and 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 and will fail hard. It will make a warning for passing signed value khổng lồ a function expecting unsigned and the guy will deprecate it to lớn not have warnings ...
share
Follow
edited May 20, 2020 at 8:16
answered May 20, 2020 at 8:07

AlexAlex
9477 bronze badges
4
add a bình luận |
1
#define min (0-0) and #define min (0) would be equivalent for type and value point of view.
It seems that it allows to break the check of your compiler.
Real fix lớn remove the warning is 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 bởi vì similar stuff for max).That cure seems not better than the warning :-/
Possible workaround would be khổng lồ wrap the kiểm tra 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;
giới thiệu
Follow
answered May 20, 2020 at 9:47

Jarod42Jarod42
193k1313 gold badges172172 silver badges278278 bronze badges
2
add a bình luận |
0
A macro applies a search and replace before compiling:
So for #define min (0) the code for the compiler will be:
if ((var >= (0)) && (var and for #define min (0-0) it will be:
if ((var >= (0-0)) && (var Both var >= 0 và 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. And using macros lớn 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: Bài Soạn Bài Luyện Tập Tóm Tắt Văn Bản Tự Sự Lớp 9 Trang 58, Soạn Bài Luyện Tập Tóm Tắt Văn Bản Tự Sự
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 to lớn handle the case when it is 0 correctly, so that the compiler is aware that you know that this case could exist.