コンテンツにスキップ

yu::tuples::get

namespace yu::tuples {
    inline namespace unspecified {
        template <std::size_t Idx>
        inline constexpr unspecified get = unspecified;
    }
}

概要

Tupleから指定した位置の要素を取得する関数オブジェクト.

効果

部分式exprの型をTexprを評価した値をtとする. このとき,式yu::tuples::get<Idx>(expr)の効果は以下の通りとなる.

  1. yu::tuples::size<T>::valueが有効な式でなければ,呼び出しは不適格.
  2. Idx < yu::tuples::size<T>::valueでなければ,呼び出しは不適格.
  3. std::remove_cvref_t<T>が要素数の判明している配列型であれば,t[Idx]と等しい.
  4. t.template get<Idx>()が有効な式であれば,t.template get<Idx>()と等しい.
  5. get<Idx>(t)getの意味がADLのみによって決まるコンテキストで,get<Idx>(t)が有効な式であれば,get<Idx>(t)と等しい.

以上のどれにも当てはまらないとき,呼び出しは不適格となる.

戻り値

TupleオブジェクトのIdx番目の要素への参照.

カスタマイゼーションポイント

4か5の条件を満たすようにする. 例えば,ユーザー定義のフリー関数テンプレートget<Idx>を定義するか,ユーザー定義のクラスにメンバ関数テンプレートget<Idx>を持たせることでカスタマイズできる.

#include <tuple>
#include <string>
#include <yu/tuples/get.hpp>

int main() {
    // std::tuple
    {
    std::tuple<int, char, std::string> tup(1, 'a', "hello");

    std::string& i = yu::tuples::get<2>(tup);
    std::cout << i << std::endl;
    }

    // 組み込み配列
    {
        int arr[3] = {1, 2, 3};

        int& i = yu::tuples::get<1>(arr);
        std::cout << i << std::endl;
    }
}

出力

hello
2