Posts

Showing posts from December 12, 2018

Get first element of std::tuple satisfying trait

Image
up vote 2 down vote favorite I'm using C++17. I'd like to get an element of a tuple that satisfies some type trait. It would be amazing if the trait could be supplied generically, but I'd be satisfied with a specific function for a certain trait. Usage might look something like this: auto my_tuple = std::make_tuple { 0.f, 1 }; auto basic = get_if_integral (my_tuple); auto fancy = get_if<std::is_floating_point> (my_tuple); std::cout << basic; // '1' std::cout << fancy; // '0.f' Ideally this would fail to compile if more than one element satisfies the trait, like std::get (std::tuple) . c++ c++17 variadic-templates template-meta-programming stdtuple share | improve this qu