In clang we can ignore warnings as described here.
In my example this works fine if the code is directly enclosed in the pragmas (see (1)). However this doesn't work at (2) because the offending code is inside a function (3). The warning is only ignored if I enclose the function with a pragma (commented out). If the function is inside a header I can also enclose the #include with a pragma.
I generally want warnings for this function but I don't want to disable them for the whole file it is used in. So is there a way to disable the warning on a per-use case?
#include <iostream>
// #pragma clang diagnostic push
// #pragma clang diagnostic ignored "-Wfloat-equal"
template <class T>
bool compare (const T lhs, const T rhs)
{
return lhs == rhs; // (3)
}
// #pragma clang diagnostic pop
int main ()
{
const float a = 1.1f, b = 1.1f;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
const bool eq1 = a == b; // (1)
#pragma clang diagnostic pop
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
const bool eq2 = compare (a, b); // (2)
#pragma clang diagnostic pop
std::cout << eq1 << " " << eq2 << std::endl;
return 0;
}
Compile with -Weverything
Aucun commentaire:
Enregistrer un commentaire