Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to silence long long integer constant warning from GCC

I have some code using large integer literals as follows:

if(nanoseconds < 1'000'000'000'000)

This gives the compiler warning integer constant is too large for 'long' type [-Wlong-long]. However, if I change it to:

if(nanoseconds < 1'000'000'000'000ll)

...I instead get the warning use of C++11 long long integer constant [-Wlong-long].

I would like to disable this warning just for this line, but without disabling -Wlong-long or using -Wno-long-long for the entire project. I have tried surrounding it with:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wlong-long"
...
#pragma GCC diagnostic pop

but that does not seem to work here with this warning. Is there something else I can try?

I am building with -std=gnu++1z.

Edit: minimal example for the comments:

#include <iostream>
auto main()->int {
  double nanoseconds = 10.0;
  if(nanoseconds < 1'000'000'000'000ll) {
    std::cout << "hello" << std::endl;
  }
  return EXIT_SUCCESS;
}

Building with g++ -std=gnu++1z -Wlong-long test.cpp gives test.cpp:6:20: warning: use of C++11 long long integer constant [-Wlong-long]

I should mention this is on a 32bit platform, Windows with MinGW-w64 (gcc 5.1.0) - the first warning does not seem to appear on my 64bit Linux systems, but the second (with the ll suffix) appears on both.

like image 808
Riot Avatar asked Oct 19 '22 22:10

Riot


1 Answers

It seems that the C++11 warning when using the ll suffix may be a gcc bug. (Thanks @praetorian)

A workaround (inspired by @nate-eldredge's comment) is to avoid using the literal and have it produced at compile time with constexpr:

int64_t constexpr const trillion = int64_t(1'000'000) * int64_t(1'000'000);
if(nanoseconds < trillion) ...
like image 101
Riot Avatar answered Oct 21 '22 15:10

Riot