How to “iterate” over a list of templates at compile time?

我们两清 提交于 2019-12-10 17:55:50

问题


This is the extraction of a follow-up question to this answer.

Given the following "loop" technique

#pragma once
// loop.hpp

#include <type_traits>
#include <utility>

template<std::size_t... indices, class LoopBody>
void loop_impl(std::index_sequence<indices...>, LoopBody&& loop_body) {
  (// C++17's fold expression
    loop_body(std::integral_constant<std::size_t, indices>{}),
    ...
  );
}

template<std::size_t N, class LoopBody>
void loop(std::integral_constant<std::size_t, N>, LoopBody&& loop_body) {
  loop_impl(std::make_index_sequence<N>{}, std::forward<LoopBody>(loop_body));
}

it is possible to iterate over a type list like this:

#include <iostream>
#include <string_view>
#include <tuple>

#include "loop.hpp"

template<class T>
std::string_view inspect() {
  return __PRETTY_FUNCTION__;
}

using Types = std::tuple<int, int, char, bool, double>;

int main() {
  loop(std::tuple_size<Types>{}, [&] (auto i) {
    using T = std::tuple_element_t<i, Types>;

    std::cout << i << ": " << inspect<T>() << "\n";
  });
}

... but how to iterate over a list of templates?


回答1:


Using Boost.Mp11, the first version is:

static constexpr auto size = std::tuple_size_v<Types>;
mp_for_each<mp_iota_c<size>>([&] (auto i) {
    /* ... */
});

Doing this over templates works basically the same way:

using list = mp_list<mp_quote<std::tuple>, mp_quote<std::pair>>;
mp_for_each<list>([&](auto f){
   // v is a tuple<int, char> the first time around and a pair<int, char>
   // the second time around
   mp_invoke_q<decltype(f), int, char> v;
});

Of course you can do whatever you want with f in the body, I just invoked it as an example of mp_quote and how well integrated it is with the rest of Mp11.




回答2:


You may wrap templates of the form template<class...> class into a tag type like the following:

#pragma once
// template_tag.hpp

#include <tuple>
#include <type_traits>

template<
  template<class...>
  class Tmpl_
>
struct TemplateTag {
  template<class... Ts>
  using insert = Tmpl_<Ts...>;

  template<
    template<template<class... > class>
    class TmplTmpl
  >
  using rewrap_into = TmplTmpl<Tmpl_>;
};

// convenience helper
template<class TmplTag, class... Ts>
using InsertTemplateArgs = typename TmplTag::template insert<Ts...>;

static_assert(
  std::is_same<
    InsertTemplateArgs< TemplateTag<std::tuple>, int, bool >,
    std::tuple<int, bool>
  >{}
);

// convenience helper
template<class TmplTag, template<template<class...> class> class TmplTmpl>
using RewrapTemplateInto = typename TmplTag::template rewrap_into<TmplTmpl>;

template<template<class...> class Tmpl>
struct OtherTemplateTag {};

static_assert(
  std::is_same<
    RewrapTemplateInto< TemplateTag<std::tuple>, OtherTemplateTag >,
    OtherTemplateTag<std::tuple>
  >{}
);

Once your templates are wrapped into a tag type, you can iterate over the types as before:

#include <iostream>
#include <string_view>
#include <tuple>
#include <utility>
#include <variant>

#include "loop.hpp"
#include "template_tag.hpp"

template<class T>
std::string_view inspect() {
  return __PRETTY_FUNCTION__;
}

using Templates = std::tuple<
  TemplateTag<std::tuple>,
  TemplateTag<std::tuple>,
  TemplateTag<std::pair>,
  TemplateTag<std::variant>
>;

template<
  template<class...>
  class Tmpl
>
struct AnotherTemplateTag {};

int main() {
  loop(std::tuple_size<Templates>{}, [&] (auto i) {
    using TmplTag = std::tuple_element_t<i, Templates>;

    std::cout << i << ": " << inspect<TmplTag>() << "\n";

    using AnotherTmplTag = RewrapTemplateInto<TmplTag, AnotherTemplateTag>;
    std::cout << "   " << inspect<AnotherTmplTag>() << "\n";

    using TmplWithArgs = InsertTemplateArgs<TmplTag, int, long>;
    std::cout << "   " << inspect<TmplWithArgs>() << "\n";
  });
}


来源:https://stackoverflow.com/questions/55090524/how-to-iterate-over-a-list-of-templates-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!