Let's say I have a pow function that I deliberately want to overflow:
#include <cstdint>
constexpr uint64_t pow(uint64_t val)
{
return val * val;
}
int main()
{
constexpr uint64_t val = pow(0x100000000);
}
This will trigger a warning 4307 (overflow warning) at every constexpr call, which makes sense--it can evaluate if each call will cause the overflow or not. However, trying to suppress the warning in the function definition doesn't seem to have any effect. We still get all the same warnings if we do this:
constexpr uint64_t pow(uint64_t val)
{
#pragma warning(suppress:4307)
return val * val;
}
Instead we have to suppress the warning every time we call the function, which is undesirable:
int main()
{
#pragma warning(suppress:4307)
constexpr uint64_t val = pow(0x100000000);
}
The best option I could find was creating a macro that wraps the call, and calling that:
#define safePow(val) \
__pragma(warning(suppress:4307)) \
pow(val)
Is this the only way to have a warning-free constexpr, short of disabling the warning globally?
Note: "integer constant overflow" warning in constexpr touches on similar topics, but the focus there was writing code to work around causing a warning, whereas I want to know specifically if the warning can be disabled somehow.
Aucun commentaire:
Enregistrer un commentaire