Как узнать тип переменной C++

Содержание
Введение
typeid().name()
В других языках
Другие статьи о С++

Введение

Для определения типа переменной в C++ существует функция typeid().name() из библиотеки typeinfo которую нужно предварительно подключить с помощью

#include <typeinfo>

typeid().name()

Небольшая демонстрация

#include <string> #include <iostream> #include <typeinfo> int main() { // Создадим четыре переменные разных типов int int_var = 1; float float_var = 1.0; char char_var = '0'; std::string str1 = "www.heihei.ru"; // Выведем на экран результат работы typeid std::cout << typeid(int_var).name() << std::endl; std::cout << typeid(float_var).name() << std::endl; std::cout << typeid(char_var).name() << std::endl; std::cout << typeid(str1).name() << std::endl; return 0; }

Результат

int float char class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > C:\Users\ao\source\repos\typeid\x64\Debug\typeid.exe (process 132920) exited with code 0. Press any key to close this window . . .

Все четыре типа определены.

Banner image

В других языках

Article author: Andrei Olegovich

Похожие статьи
Development на C++
Перегрузка функций
-c: Компиляция
Разбиение кода на части
Вектор
Указатели
Классы
SFML
Тетрис на C++ с библиотекой SFML2
SDL
Массив Структур
Решение задач на C++
Как создать пустую строку в C++
Запросы к REST API на C++
Ошибки C++
Make

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: