0

だから私はこのようなテンプレートタイプのリストを持っています:

template <typename... Types>
struct type_list
{
};

次のようなアクセサ関数を作成しました。

template<class TypeList, size_t ElementIndex>
struct at;

template <template<typename...> class TypeList, typename Head, typename... OtherTypes, size_t ElementIndex>
struct at<  TypeList<Head, OtherTypes...>,  ElementIndex>
{
    static_assert(ElementIndex < (size_v<   TypeList<Head, OtherTypes...>   >), "at_t : ElementIndex is bigger than list size");

    using type = if_else_t < ElementIndex == 0, Head, typename at<  TypeList<OtherTypes...>, ElementIndex - 1   >::type >;
};

template <template<typename...> class TypeList, typename Last, size_t ElementIndex>
struct at<  TypeList<Last>, ElementIndex>
{
    static_assert(ElementIndex < (size_v<   TypeList<Last>  >), "at_t : ElementIndex is bigger than list size");

    using type = Last;
};

template<class TypeList, size_t ElementIndex>
using at_t = typename at<TypeList, ElementIndex>::type;

これif_else_t<>には次の実装があります。

template<bool Condition, typename True, typename False>
struct if_else
{
    using type = True;
};

template<typename True, typename False>
struct if_else<false, True, False>
{
    using type = False;
};

今、関数を次のようにテストすると:

static_assert(std::is_same_v<   bool, at_t< type_list<int, float, bool, char>, 2    >   >, "at_t : Bad result");

ElementIndex がリスト サイズより大きいかどうかをチェックする static_assert をトリガーします。at<>コンパイラの出力から、 ElementIndex が数値制限 (ElementIndex = 0 - 1 の場合) に達し、static_assert がトリガーされるまで、再帰が停止しないことがはっきりとわかります。

私は何を間違っていますか?

理想的な答えには、より優れた、よりエレガントな実装も含める必要がありますat<>:)

MSVC と C++17 を使用していることに注意してください。

4

2 に答える 2