GCC: How To Suppress a Warning in a Single Line of Code
TIL that GCC allows to suppress a given type of warnings in a single line of code. Here is an example:
InMemoryStorage::InMemoryStorage() {
if(pthread_rwlock_init(&_rwlock, NULL) != 0)
throw std::runtime_error("pthread_rwlock_init() failed");
}
InMemoryStorage::~InMemoryStorage() {
if(pthread_rwlock_destroy(&_rwlock) != 0) {
// suppress 'throw will always call terminate() [-Wterminate]'
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wterminate"
throw std::runtime_error("pthread_rwlock_destroy() failed");
#pragma GCC diagnostic pop
}
}
I think that using this feature was justified in this specific case. Thank you,
GCC, for warning me that throwing an exception in a destructor will cause a
program to terminate. However, it still looks like something I want. If
pthread_rwlock_destroy
fails, it means that something is most likely to be
very wrong (memory corruption or something like this).
Comments