Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbe460bc

Browse files
committed
make C macros evaluate arguments once; reduce unnecessary parentheses
1 parent57dec20 commitbe460bc

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

‎api/Common.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ typedef enum {
4444
#ifdef __cplusplus
4545

4646
template<classT,classL>
47-
automin(const T& a,const L& b) -> decltype((b < a) ? b : a)
47+
automin(const T& a,const L& b) -> decltype(b < a ? b : a)
4848
{
4949
return (b < a) ? b : a;
5050
}
5151

5252
template<classT,classL>
53-
automax(const T& a,const L& b) -> decltype((b < a) ? b : a)
53+
automax(const T& a,const L& b) -> decltype(b < a ? b : a)
5454
{
5555
return (a < b) ? b : a;
5656
}
5757

5858

5959
template<classT,classU,classV>
60-
autoconstrain(const T& amt,const U& low,const V& high) -> decltype((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
60+
autoconstrain(const T& amt,const U& low,const V& high) -> decltype(amt <low ?low : (amt >high ?high :amt))
6161
{
62-
return(amt)<(low)?(low):((amt)>(high)?(high):(amt));
62+
return amt <low ?low : (amt >high ?high :amt);
6363
}
6464

6565
template<classT>
@@ -81,19 +81,29 @@ typedef enum {
8181
}
8282
#else
8383
#ifndef constrain
84-
#defineconstrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
84+
#defineconstrain(amt,low,high) \
85+
({__typeof__ (amt) _amt = (amt); \
86+
__typeof__ (low) _low = (low); \
87+
__typeof__ (high) _high = (high); \
88+
_amt < _low ? _low : (_amt > _high ? _high :_amt); }
8589
#endif
8690

8791
#ifndef radians
88-
#defineradians(deg) ((deg)*DEG_TO_RAD)
92+
#defineradians(deg) \
93+
({__typeof__ (deg) _deg = deg; \
94+
_deg * DEG_TO_RAD; })
8995
#endif
9096

9197
#ifndef degrees
92-
#definedegrees(rad) ((rad)*RAD_TO_DEG)
98+
#definedegrees(rad) \
99+
({__typeof__ (rad) _rad = rad; \
100+
_rad * RAD_TO_DEG; })
93101
#endif
94102

95103
#ifndef sq
96-
#definesq(x) ((x)*(x))
104+
#definesq(x) \
105+
({__typeof__ (x) _x = x; \
106+
_x * _x; })
97107
#endif
98108

99109
#ifndef min

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp