Home > database >  Structural tuple-like / Wrapping a variadic set of template parameters values in a struct
Structural tuple-like / Wrapping a variadic set of template parameters values in a struct

Time:09-16

I find myself wanting/needing to use a variadic composite type as a template parameter. Unfortunately, std::tuple<> is not a structural type, which makes the obvious approach a no-go:

#include <tuple>

template<typename... Ts>
struct composite {
  std::tuple<Ts...> operands;
};

template<auto v>
void foo() {}

int main() {
  constexpr composite tmp{std::make_tuple(1,2,3)};
  foo<tmp>();  // <----- Nope!
}

Is there a reasonable way to build such a composite in a manner that works as a structural type?

Since the MCVE in isolation is trivially solvable as "just make foo() a variadic template", here's a more representative example:

On godbolt

#include <concepts>
#include <tuple>

template <typename T, template <typename...> typename U>
concept TemplatedConfig = requires(T x) {
  { U(x) } -> std::same_as<T>;
  // A few more things identifying T as a valid config
};

template<auto Config>
struct proc;

// Basic
struct Basic {};

template<Basic v>
struct proc<v> {
    constexpr int foo() { return 0; }
};

// Annotated
template<typename T>
struct Annotated {
    T v;
    int annotation;
};

template<TemplatedConfig<Annotated> auto v>
struct proc<v> {
    constexpr int foo() { return 1; }
};

// ... more config / specialization pairs ...

// Composite
template<typename... Parts>
struct Composite {
    std::tuple<Parts...> parts;
};

template<TemplatedConfig<Composite> auto v>
struct proc<v> {
    constexpr int foo() { return 2; }
};


int main() {
    constexpr Basic a = Basic{};
    constexpr Annotated b{a, 12};
    constexpr Composite c{std::make_tuple(a, b)};

    static_assert(proc<a>{}.foo() == 0);
    static_assert(proc<b>{}.foo() == 1);
    static_assert(proc<c>{}.foo() == 2);   <----- :(
}

Edit: If anyone is curious what a (almost) fully-realized tuple class based on the accepted answer looks like: https://gcc.godbolt.org/z/ThaGjbo67

CodePudding user response:

Create your own structural tuple-like class? Something like:

template <std::size_t I, typename T>
struct tuple_leaf
{
    T data;    
};

template <typename T> struct tag{ using type = T; };

template <typename Seq, typename...>
struct tuple_impl;

template <std::size_t... Is, typename... Ts>
struct tuple_impl<std::index_sequence<Is...>, Ts...> : tuple_leaf<Is, Ts>...
{
    constexpr tuple_impl(Ts... args) : tuple_leaf<Is, Ts>{args}... {}
};

template <typename T, std::size_t I> constexpr const T& get(const tuple_leaf<I, T>& t) { return t.data; }
template <typename T, std::size_t I> constexpr T& get(tuple_leaf<I, T>& t) { return t.data; }
template <std::size_t I, typename T> constexpr const T& get(const tuple_leaf<I, T>& t) { return t.data; }
template <std::size_t I, typename T> constexpr T& get(tuple_leaf<I, T>& t) { return t.data; }


template <std::size_t I, typename T>
tag<T> tuple_element_tag(const tuple_leaf<I, T>&);

template <std::size_t I, typename Tuple>
using tuple_element = decltype(tuple_element_tag<I>(std::declval<Tuple>()));

template <std::size_t I, typename Tuple>
using tuple_element_t = typename tuple_element<I, Tuple>::type;

template <typename ... Ts>
using tuple = tuple_impl<std::make_index_sequence<sizeof...(Ts)>, Ts...>;

Demo

  • Related