How to do portable 64 bit arithmetic, without compiler warnings

后端 未结 7 1011
情歌与酒
情歌与酒 2021-02-12 22:38

I occasionally use 64 bit arithmetic in an open source C++ library of mine. I discovered that long long serves my purpose quite nicely. Even some 10 year old solari

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-12 23:32

    You can also suppress the warning using gcc's "__extension__" feature, e.g.:

    // No '-pedantic' warning/error.
    __extension__ long long foo = 2;
    
    // Exhibits '-pedantic' warning/error.
    long long bar = 3
    

    and the compile:

    $ g++ -pedantic -fsyntax-only foo.cpp
    foo.cpp:5: error: ISO C++ 1998 does not support 'long long'
    

    Notice that only the last use of long long triggered the -pedantic error since no __extension__ was prepended. Regardless, I'd go with @Steve Jessop's suggestion of using int64_t instead.

提交回复
热议问题