Программы на C++. Форум разработчиков
Anonymous
Получить тип первого элемента структурированного типа
Сообщение
Anonymous » 03 сен 2025, 18:39
Я хочу получить тип первого элемента любых видов структурированных типов.
Код: Выделить всё
#include
#include
#include
#include
#include
template < typename T>
void Print()
{
std::cout
void Funny( const T& t )
{
using ELEMENT_TYPE = decltype( GetFirst(*(std::declval().begin())) );
Print< ELEMENT_TYPE> ();
}
std::vector> s1{ {1, 2.9} };
struct T2 { std::string s; bool b; };
std::array s2{ "Hallo", true};
int main()
{
Funny( s1 );
Funny( s2 );
}
< /code>
, который приводит к: < /p>
void Print() [with T = int]
void Print() [with T = std::__cxx11::basic_string]
Код работает, как и ожидалось.>
Подробнее здесь:
https://stackoverflow.com/questions/797 ... tured-type
1756913991
Anonymous
Я хочу получить тип первого элемента любых видов структурированных типов.[code]#include #include #include #include #include template < typename T> void Print() { std::cout void Funny( const T& t ) { using ELEMENT_TYPE = decltype( GetFirst(*(std::declval().begin())) ); Print< ELEMENT_TYPE> (); } std::vector> s1{ {1, 2.9} }; struct T2 { std::string s; bool b; }; std::array s2{ "Hallo", true}; int main() { Funny( s1 ); Funny( s2 ); } < /code> , который приводит к: < /p> void Print() [with T = int] void Print() [with T = std::__cxx11::basic_string] [/code] Код работает, как и ожидалось.> Подробнее здесь: [url]https://stackoverflow.com/questions/79754443/get-type-of-first-element-of-structured-type[/url]