我希望在 C 中实现通用的临时重试机制(仅重试一定次数)。我正在寻找与 GNU TEMP_FAILURE_RETRY
宏类似的功能。
我目前拥有的:
#define TEMP_RETRY_COUNT 10
#define TEMP_RETRY( exp ) \
({ \
int _attemptc_ = TEMP_RETRY_COUNT; \
bool _resultb_; \
while ( _attemptc_-- ) \
if ( _resultb_ = exp ) break; \
_resultb_; \
})
工作正常。我现在正试图抑制编译器警告的警告,并寻找更干净的东西:
bleh.c: In function ‘main’:
bleh.c:38:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if ( TEMP_RETRY( bleh() ) )
^
bleh.c:46:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
TEMP_RETRY( bleh() );
感谢您的任何回复!它不一定是一个宏。此外,可以假定 exp
返回 bool 值(或等效值)。
最佳答案
愚蠢的我:
#define TEMP_RETRY_COUNT 10
#define TEMP_RETRY( exp ) \
({ \
int _attemptc_ = TEMP_RETRY_COUNT; \
bool _resultb_; \
while ( _attemptc_-- ) \
if ( (_resultb_ = exp) ) break; \
_resultb_; \
})
关于c - C 中的通用临时重试宏/函数 (TEMP_FAILURE_RETRY),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52102745/