TGViewer
C++: Хроники Дурки🚑 C++: Хроники Дурки🚑 @cpp_durka · 916 subscribers
Post #52 1.49K
Итак, у нас, согласно cppreference, c 11-ого стандарта есть набор целочисленных типов типа:


int8_t
int16_t
int32_t
int64_t

signed integer type with width of exactly 8, 16, 32 and 64 bits respectively
with no padding bits and using 2's complement for negative values
(provided if and only if the implementation directly supports the type)



Ну давайте поиграемся.

Что будет выведено вот на это:


std::cout << "int16_t: \n"
<< static_cast<int16_t>(00) << '\n'
<< static_cast<int16_t>(48) << '\n'
<< static_cast<int16_t>(65) << '\n'
<< std::endl;


Правильный ответ:

```
int16_t:
0
48
65
```


А вот на это?


std::cout << "int64_t: \n"
<< static_cast<int64_t>(00) << '\n'
<< static_cast<int64_t>(48) << '\n'
<< static_cast<int64_t>(65) << '\n'
<< std::endl;


Правильный ответ:

```
int64_t:
0
48
65
```

А вот на это?


std::cout << "int8_t: \n"
<< static_cast<int8_t>(00) << '\n'
<< static_cast<int8_t>(48) << '\n'
<< static_cast<int8_t>(65) << '\n'
<< std::endl;


Правильный ответ:

```
int8_t:
0
A
```


А все почему?

Потому что идите все нахер, int8_t - это char.


Особенно это приятно, когда у вас из логов пропадает что-то такое:



enum class Direction : int8_t {
LEFT, RIGHT, UP, DOWN
};

// ...



std::cout << static_cast<int8_t>(Direction::LEFT)
<< std::endl;

using Int = std::underlying_type_t<Direction>;
std::cout << static_cast<Int>(Direction::LEFT)
<< std::endl;




Ну вот и нахрен так жить?
godbolt.org Compiler Explorer - C++ enum class Direction : int8_t { LEFT, RIGHT, UP, DOWN }; int main() { std::cout << "int16_t: \n" << static_cast<int16_t>(00) << '\n' << static_cast<int16_t>(48) << '\n' << static_cast<int16_t>(65) << '\n'…
  • 😁 23
  • 👍 4
  • 🔥 1
  • 💊 1
More from @cpp_durka
  1. Sep 8, 2026Мошенники заставили пенсионерку из Москвы переписать квартиру на Rust
  2. Aug 31, 2026Всяко разное болезненное есть в С++, из всего болезненного одно из самых болезненных - это…
  3. Aug 24, 2026Если кто не знаком с библиотекой nlohmann/json - она прекрасна. Мои мысли о том, как должн…
  4. Aug 21, 2026#толькосвоимемы Код взят тут. if (dim == 0) idx_dim = i0; else if (dim == 1) idx_dim = i1;…
  5. Aug 17, 2026Ладно, искать баги в компиляторах весело, но недостаточно. Давайте поиграемся в чуть более…
  6. Aug 10, 2026Ладно, разумеется прошлый пост был набросом. Никогда не обновляйте компиляторы без очень с…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →