How is std::tuple implemented?

前端 未结 4 478
臣服心动
臣服心动 2021-01-31 16:03

I\'d like to know how are tuple implemented in standard library for C++0x. I tried to read description in libstdc++ manual and then read template listing, but it\'s really hard

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 16:50

    A tuple is typically implemented as a compile time linked-list.

    The code is a bit obfuscated through template-syntax, but following elements are normally present:

    1. a chain of classes with head and tail elements (cons-elements)
    2. an empty tail instance to indicate the end of the list.
    3. recursive code to walk the list to a certain index, implemented as recursive template-instantiations (instantiated at compile time).

    There exist reasonable implementations in C++03 (e.g. boost).

    Variadic templates allow an unlimited number of elements, as mentioned by Motti.

    The cost is normally a compile time-one. Copy constructors might be called during initialization (max 1), and when copying the tuples themselves.

提交回复
热议问题