C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fwd: Extension to runtime polymorphism proposed

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 10 Apr 2026 08:39:21 -0700
On Thursday, 9 April 2026 23:30:10 Pacific Daylight Time Muneem via Std-
Proposals wrote:
> template <std::size_t N, typename type_set, typename... Tail>
> class heterogeneous_list_impl;
>
> template <std::size_t N, typename Head_t, typename... Tail>
> class heterogeneous_list<N, Head_t, Tail...> : public
> heterogeneous_list_impl<N - 1,Element_t<N, Head_t, Tail...>,Tail...> {
> Head_t Element;
> public:
> using type_set = Element_t<N, Head_t, Tail...>;
>
> type_set operator[](std::size_t index) {
> if (index == N) {
> return type_set(Element);
> } else if constexpr (N > 0) {
> return this->heterogeneous_list_impl<N - 1, type_set,
> Tail...>::operator[](index);
> } else {
> throw std::string{"out of bound access"};
> }
> }
> };

You do realise this is just std::variant with one extra operator, right?

That operator[] could easily be implemented in the current std::variant, with
a return either of a new type such as variant_element_ref or simply
std::variant<std::add_reference_t<Types>...>. That return type is your
innovation, not the heterogeneous list type (which is not a list).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-04-10 15:39:29