# int64_t Literal Constant?

事情源於一個 error:

```cpp
#include <algorithm>
#include <iostream>

int main() {
    int64_t a = 2;
    std::cout << std::min(1L, a) << std::endl;  // mac 上編不過
    std::cout << std::min(1LL, a) << std::endl; // ubuntu 20.04 上編不過
    return 0;
}
```

無論是 1L 還是 1LL 都沒辦法作為跨平台的通用 int64\_t literal。那該怎麼辦？

## INT64\_C

```cpp
#include <cstdint>

std::cout << std::min(INT64_C(1), a) << std::endl;
```

> expands to an integer constant expression having the value specified by its argument and whose type is the [promoted](https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_promotion) type of std::int\_least8\_t, std::int\_least16\_t, std::int\_least32\_t and std::int\_least64\_t respectively

根據 Document ，他的語意是「至少 64 bits 的 integer」，這是沒有保證他「就是 64bits」，所以在這裡是不能用的。

## static\_cast

```cpp
std::cout << std::min(static_cast<int64_t>(1), a) << std::endl;
```

看起來有一點囉唆，但是既可以符合語意，也可以 work。

## constant

```cpp
const int64_t XXX_MIN = 1;

std::cout << std::min(XXX_MIN, a) << std::endl;
```

假如常數很有意義的話，也不是不行？

## Ref

[https://stackoverflow.com/questions/78183946/should-i-use-int64-c-as-int64-t-literal](https://stackoverflow.com/questions/78183946/should-i-use-int64-c-as-int64-t-literal)
