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/1096
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/1096

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Telegram announces Anonymous Admins

The cloud-based messaging platform is also adding Anonymous Group Admins feature. As per Telegram, this feature is being introduced for safer protests. As per the Telegram blog post, users can “Toggle Remain Anonymous in Admin rights to enable Batman mode. The anonymized admin will be hidden in the list of group members, and their messages in the chat will be signed with the group name, similar to channel posts.”

The global forecast for the Asian markets is murky following recent volatility, with crude oil prices providing support in what has been an otherwise tough month. The European markets were down and the U.S. bourses were mixed and flat and the Asian markets figure to split the difference.The TSE finished modestly lower on Friday following losses from the financial shares and property stocks.For the day, the index sank 15.09 points or 0.49 percent to finish at 3,061.35 after trading between 3,057.84 and 3,089.78. Volume was 1.39 billion shares worth 1.30 billion Singapore dollars. There were 285 decliners and 184 gainers.

telegram from us


Telegram C++ Academy
FROM USA