Telegram Group & Telegram Channel
🔍 C++ Задача для профи: кто вызовется?

У тебя есть следующий код:


#include <iostream>
#include <type_traits>

template<typename T>
typename std::enable_if<std::is_integral<T>::value, void>::type
process(T value) {
std::cout << "Integral: " << value << std::endl;
}

template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, void>::type
process(T value) {
std::cout << "Floating-point: " << value << std::endl;
}

int main() {
process(42); // (1)
process(3.14); // (2)
process('A'); // (3)
}


Вопросы:
1. Что напечатает программа?
2. Почему вызов process('A') может удивить?
3. Что произойдёт, если передать true?

🧠 Подвох:

- char — это integral type, а не string и не отдельный класс
- true — это тоже int`-подобный тип: `std::is_integral<bool>::value == true
- У тебя может возникнуть разный вывод в зависимости от перегрузки

Разбор:

- `process(42)` → `Integral: 42`
- `process(3.14)` → `Floating-point: 3.14`
- `process('A')` → `Integral: 65` (ASCII код символа!) ⚠️
- `process(true)` → `Integral: 1` (да, это `bool`)

🎯 Что проверяет задача:

- Понимание `SFINAE` и `enable_if`
- Типовую систему C++ (что именно считается integral)
- Разницу между `char`, `bool`, `int`, `float` в шаблонах
- Предсказание поведения перегрузки на уровне типов

@cpluspluc



tg-me.com/cpluspluc/1089
Create:
Last Update:

🔍 C++ Задача для профи: кто вызовется?

У тебя есть следующий код:


#include <iostream>
#include <type_traits>

template<typename T>
typename std::enable_if<std::is_integral<T>::value, void>::type
process(T value) {
std::cout << "Integral: " << value << std::endl;
}

template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, void>::type
process(T value) {
std::cout << "Floating-point: " << value << std::endl;
}

int main() {
process(42); // (1)
process(3.14); // (2)
process('A'); // (3)
}


Вопросы:
1. Что напечатает программа?
2. Почему вызов process('A') может удивить?
3. Что произойдёт, если передать true?

🧠 Подвох:

- char — это integral type, а не string и не отдельный класс
- true — это тоже int`-подобный тип: `std::is_integral<bool>::value == true
- У тебя может возникнуть разный вывод в зависимости от перегрузки

Разбор:

- `process(42)` → `Integral: 42`
- `process(3.14)` → `Floating-point: 3.14`
- `process('A')` → `Integral: 65` (ASCII код символа!) ⚠️
- `process(true)` → `Integral: 1` (да, это `bool`)

🎯 Что проверяет задача:

- Понимание `SFINAE` и `enable_if`
- Типовую систему C++ (что именно считается integral)
- Разницу между `char`, `bool`, `int`, `float` в шаблонах
- Предсказание поведения перегрузки на уровне типов

@cpluspluc

BY C++ Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/cpluspluc/1089

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

The Singapore stock market has alternated between positive and negative finishes through the last five trading days since the end of the two-day winning streak in which it had added more than a dozen points or 0.4 percent. The Straits Times Index now sits just above the 3,060-point plateau and it's likely to see a narrow trading range on Monday.

The seemingly negative pandemic effects and resource/product shortages are encouraging and allowing organizations to innovate and change.The news of cash-rich organizations getting ready for the post-Covid growth economy is a sign of more than capital spending plans. Cash provides a cushion for risk-taking and a tool for growth.

telegram from hk


Telegram C++ Academy
FROM USA