Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/tuple/tuple element"

From cppreference.com
< cpp‎ | utility‎ | tuple
(Example: +demangle, MSVC output is no longer valuable)
m (Example: further reduce the printer code with `boost`.)
 
Line 47: Line 47:
 
{{example
 
{{example
 
|code=
 
|code=
#include <boost/core/demangle.hpp>
+
#include <boost/type_index.hpp>
 
#include <cstddef>
 
#include <cstddef>
 
#include <iostream>
 
#include <iostream>
 
#include <string>
 
#include <string>
 
#include <tuple>
 
#include <tuple>
#include <typeinfo>
 
#include <type_traits>
 
 
#include <utility>
 
#include <utility>
 
namespace details
 
{
 
    template<class TupleLike, std::size_t I>
 
    void printTypeAtIndex()
 
    {
 
        using SelectedType = std::tuple_element_t<I, TupleLike>;
 
        using RemovedRefType = std::remove_reference_t<SelectedType>;
 
 
        std::cout << "  The type at index " << I << " is: ";
 
        std::cout << boost::core::demangle(typeid(SelectedType).name());
 
        if constexpr (std::is_const_v<RemovedRefType>)
 
            std::cout << " const";
 
        if constexpr (std::is_volatile_v<RemovedRefType>)
 
            std::cout << " volatile";
 
        if constexpr (std::is_lvalue_reference_v<SelectedType>)
 
            std::cout << '&';
 
        if constexpr (std::is_rvalue_reference_v<SelectedType>)
 
            std::cout << "&&";
 
        std::cout << '\n';
 
    }
 
}
 
  
 
template<typename TupleLike, std::size_t I = 0>
 
template<typename TupleLike, std::size_t I = 0>
Line 82: Line 58:
 
{
 
{
 
     if constexpr (I == 0)
 
     if constexpr (I == 0)
         std::cout << boost::core::demangle(typeid(TupleLike).name()) << '\n';
+
         std::cout << boost::typeindex::type_id_with_cvr<TupleLike>() << '\n';
  
 
     if constexpr (I < std::tuple_size_v<TupleLike>)
 
     if constexpr (I < std::tuple_size_v<TupleLike>)
 
     {
 
     {
         details::printTypeAtIndex<TupleLike, I>();
+
         using SelectedType = std::tuple_element_t<I, TupleLike>;
 +
 
 +
        std::cout << "  The type at index " << I << " is: "
 +
                  << boost::typeindex::type_id_with_cvr<SelectedType>() << '\n';
 
         printTypes<TupleLike, I + 1>();
 
         printTypes<TupleLike, I + 1>();
 
     }
 
     }

Latest revision as of 08:25, 4 October 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <tuple>
template< std::size_t I, class... Types >
struct tuple_element< I, std::tuple<Types...> >;
(since C++11)

Provides compile-time indexed access to the types of the elements of the tuple.

Contents

[edit] Member types

Type Definition
type the type of Ith element of the tuple, where I is in [0sizeof...(Types))

[edit] Possible implementation

template<std::size_t I, class T>
struct tuple_element;
 
#ifndef __cpp_pack_indexing
// recursive case
template<std::size_t I, class Head, class... Tail>
struct tuple_element<I, std::tuple<Head, Tail...>>
    : std::tuple_element<I - 1, std::tuple<Tail...>>
{ };
 
// base case
template<class Head, class... Tail>
struct tuple_element<0, std::tuple<Head, Tail...>>
{
    using type = Head;
};
 
#else
// C++26 implementation using pack indexing
template<std::size_t I, class... Ts>
struct tuple_element<I, std::tuple<Ts...>>
{
    using type = Ts...[I];
};
#endif

[edit] Example

#include <boost/type_index.hpp>
#include <cstddef>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
 
template<typename TupleLike, std::size_t I = 0>
void printTypes()
{
    if constexpr (I == 0)
        std::cout << boost::typeindex::type_id_with_cvr<TupleLike>() << '\n';
 
    if constexpr (I < std::tuple_size_v<TupleLike>)
    {
        using SelectedType = std::tuple_element_t<I, TupleLike>;
 
        std::cout << "  The type at index " << I << " is: "
                  << boost::typeindex::type_id_with_cvr<SelectedType>() << '\n';
        printTypes<TupleLike, I + 1>();
    }
}
 
struct MyStruct {};
 
using MyTuple = std::tuple<int, long&, const char&, bool&&,
                           std::string, volatile MyStruct>;
 
using MyPair = std::pair<char, bool&&>;
 
static_assert(std::is_same_v<std::tuple_element_t<0, MyPair>, char>);
static_assert(std::is_same_v<std::tuple_element_t<1, MyPair>, bool&&>);
 
int main()
{
    printTypes<MyTuple>();
    printTypes<MyPair>();
}

Possible output:

std::tuple<int, long&, char const&, bool&&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, MyStruct volatile>
  The type at index 0 is: int
  The type at index 1 is: long&
  The type at index 2 is: char const&
  The type at index 3 is: bool&&
  The type at index 4 is: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
  The type at index 5 is: MyStruct volatile
std::pair<char, bool&&>
  The type at index 0 is: char
  The type at index 1 is: bool&&

[edit] See also

Structured binding (C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
obtains the element types of a tuple-like type
(class template) [edit]