C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extension to std::tuples to allow runtime indexing.

From: Muneem <itfllow123_at_[hidden]>
Date: Sun, 19 Apr 2026 14:27:57 +0500
I also updated my proposal to include this example. If anyone has any
issues with it, please tell me, and I will answer everyone tomorrow and
also all the other questions that I haven't answered yet.

On Sun, Apr 19, 2026 at 1:46 PM Muneem <itfllow123_at_[hidden]> wrote:

> The previous code that I posted worked on visual studio 2026, but couldnt
> on gdb online debugger, but here version that works on gdb as well:
> https://onlinegdb.com/IBsomUfmPI
> #include <iostream>
> #include <type_traits>
> #include <variant>
> #include <functional>
> #include <array>
> template<typename Head, typename ... Types>
> struct tuple_impl :tuple_impl<Types...>
> {
> Head head;
> tuple_impl(Head&& a, Types... types) : tuple_impl<Types...>(types...),
> head{ a } {}
> tuple_impl(Head& a, Types... types) : tuple_impl<Types...>(types...),
> head{ a } {}
> };
> template<typename Head>
> struct tuple_impl<Head> {
> Head head;
> tuple_impl(Head&& a) : head{ a } {}
> tuple_impl(Head& a) : head{ a } {}
> };
>
> template<typename T, typename Head_t>
> const constexpr bool Is_unique() { // requires() couldnt work here for
> overloading
> return true;
> }
>
> template<typename T, typename Head_t, typename... Tail>
> const constexpr std::enable_if_t < (sizeof...(Tail) > 0), bool >
> Is_unique() {
> return
> !((std::is_same_v<T, Head_t>) && (Is_unique<T, Tail...>()))
> &&
> ((std::is_same_v<T, Head_t>) || (Is_unique<T, Tail...>()));
> }
>
>
> template<typename Head_t, typename ... Types >
> constexpr bool is_unique_from_after_on() {
> return Is_unique<Head_t, Types...>();
> };
> template<typename Head_t>
> constexpr bool is_unique_from_after_on() {
> return true;
> }
>
>
>
> template<typename Head, typename ... Types>
> struct tuple:tuple_impl<Types>... {
> Head head;
>
> using complex_elem = std::variant <
> std::enable_if_t<
> is_unique_from_after_on<Head, Types...>(), Head*
> >,
> std::enable_if_t <
> is_unique_from_after_on<Types...>(), Types*
> >...
> >;
> std::array<complex_elem, (sizeof...(Types))+1> book_keeper;
> template<typename Head_, typename ... Types_>
> constexpr tuple(Head_&& a, Types_&&... b ) :head{
> std::forward<Head>(a) }, tuple_impl<Types>{std::forward<Types>(b)}...,
> book_keeper{
> &head, &(this->tuple_impl<Types>::head)...}
> {
>
> }
> complex_elem operator[](size_t index) {
> return book_keeper[index];
> }
> };
> template<typename Head>
> struct tuple<Head>{
> Head head;
> tuple(Head&& a ) : head{ a } {}
> tuple(Head& a) : head{ a } {}
> std::variant<Head*> operator[](size_t index) {
> return head;
> }
> };
>
>
>
> int main() {
> tuple<int, double, float> list{1, 2.0, 3.0f};
> std::cout << *std::get<0>(list[0]) << std::endl;
> std::cout << *std::get<1>(list[1]) << std::endl;
> std::cout << *std::get<2>(list[2]) << std::endl;
> return 0;
> }
>
> //the reason it didn't work was template parameter name shadowing, but it
> still worked in visual studio 2026
>
> On Sun, Apr 19, 2026 at 1:01 PM Muneem <itfllow123_at_[hidden]> wrote:
>
>> Sorry for taking so much time for such a basic implementation, and I
>> won't be able to answer any questions until tomorrow because I am really
>> tired. I hope that we can continue this discussion tomorrow. I am sorry for
>> being hesitant to give out a proper implementation sooner.
>> This is the example implementation code:
>>
>> #include <iostream>#include <type_traits>#include <variant>#include <functional>#include <array>template<typename Head, typename ... Types>struct tuple_impl :tuple_impl<Types...>
>> {
>> Head head;
>> tuple_impl(Head&& a, Types... types) : tuple_impl<Types...>(types...), head{ a } {}
>> tuple_impl(Head& a, Types... types) : tuple_impl<Types...>(types...), head{ a } {}
>> };template<typename Head>struct tuple_impl<Head> {
>> Head head;
>> tuple_impl(Head&& a) : head{ a } {}
>> tuple_impl(Head& a) : head{ a } {}
>> };
>> template<typename T, typename Head_t>const constexpr bool Is_unique() { // requires() couldnt work here for overloading
>> return true;
>> }
>> template<typename T, typename Head_t, typename... Tail>const constexpr std::enable_if_t < (sizeof...(Tail) > 0), bool > Is_unique() {
>> return
>> !((std::is_same_v<T, Head_t>) && (Is_unique<T, Tail...>()))
>> &&
>> ((std::is_same_v<T, Head_t>) || (Is_unique<T, Tail...>()));
>> }
>>
>> template<typename Head_t, typename ... Types >constexpr bool is_unique_from_after_on() {
>> return Is_unique<Head_t, Types...>();
>> };template<typename Head_t>constexpr bool is_unique_from_after_on() {
>> return true;
>> }
>>
>>
>> template<typename Head, typename ... Types>struct tuple:tuple_impl<Types>... {
>> Head head;
>>
>> using complex_elem = std::variant <
>> std::enable_if_t<
>> is_unique_from_after_on<Head, Types...>(), Head*
>> >,
>> std::enable_if_t <
>> is_unique_from_after_on<Types...>(), Types*
>> >...
>> >;
>> std::array<complex_elem, (sizeof...(Types))+1> book_keeper;
>> template<typename Head, typename ... Types>
>> constexpr tuple(Head&& a, Types&&... b ) :head{ std::forward<Head>(a) }, tuple_impl<Types>{std::forward<Types>(b)}...,
>> book_keeper{
>> &head, &(this->tuple_impl<Types>::head)...}
>> {
>>
>> }
>> complex_elem operator[](size_t index) {
>> return book_keeper[index];
>> }
>> };template<typename Head>struct tuple<Head>{
>> Head head;
>> tuple(Head&& a ) : head{ a } {}
>> tuple(Head& a) : head{ a } {}
>> std::variant<Head*> operator[](size_t index) {
>> return head;
>> }
>> };
>>
>>
>> int main() {
>> tuple<int, double, float> list{1, 2.0, 3.0f};
>> std::cout << *std::get<0>(list[0]) << std::endl;
>> std::cout << *std::get<1>(list[1]) << std::endl;
>> std::cout << *std::get<2>(list[2]) << std::endl;
>> return 0;
>> }
>>
>>
>>
>> On Sat, Apr 18, 2026 at 9:48 PM Jonathan Wakely via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>>
>>>
>>> On Sat, 18 Apr 2026, 11:01 Giuseppe D'Angelo via Std-Proposals, <
>>> std-proposals_at_[hidden]> wrote:
>>>
>>>> Hello,
>>>>
>>>> On 18/04/2026 05:04, Muneem via Std-Proposals wrote:
>>>> > I updated my proposal:https://docs.google.com/document/d/1srd5nKI-
>>>> > QZy0vpUdkQoD45JyKIDWhom8/edit?
>>>> > usp=sharing&ouid=114869935649093915681&rtpof=true&sd=true <https://
>>>> > docs.google.com/document/d/1srd5nKI-QZy0vpUdkQoD45JyKIDWhom8/edit?
>>>> > usp=sharing&ouid=114869935649093915681&rtpof=true&sd=true>
>>>> > Thank you a lot to everyone for giving their feedback that led me to
>>>> > refine my proposal
>>>>
>>>> First and foremost: this isn't a proposal. It's just an abstract idea.
>>>> A
>>>> proposal has to go in detail: discuss tradeoffs, provide a reference
>>>> implementation, tentative wording.
>>>>
>>>>
>>>> In 2.1 there's this claim:
>>>>
>>>> > Existing tuples cannot be optimized for runtime indexing without
>>>> breaking the Application Binary Interface (ABI). Furthermore, switch
>>>> statements are not guaranteed to be the fastest option for tuples with many
>>>> elements.
>>>>
>>>> Could you show some data supporting this statement?
>>>>
>>>> Do you have a prototype implementation where you add such runtime
>>>> `get(index)` to an existing std::tuple implementation, as well as a
>>>> prototype implementation of the proposed runtime_index_tuple, and show
>>>> that the new class brings significant performance gains (... that can't
>>>> otherwise be achieved without breaking std::variant's ABI)?
>>>>
>>>> > This proposal introduces a specialization for std::variant<T&...>
>>>> with the following properties:
>>>> > No Valueless State: The variant cannot be valueless by exception.
>>>> > Immutable Type Selection: Every std::variant<T&...> is constructed
>>>> with a reference to a type T that it holds for its entire lifetime.
>>>> > Assignment Logic: Assignment modifies the underlying value referred
>>>> to by the variant rather than changing the active type.
>>>>
>>>> 1) These are extraordinary claims that aren't backed by extraordinary
>>>> facts.
>>>>
>>>> 2) Adding support for references in std::variant deserves its own
>>>> paper.
>>>> It's something that was there originally and then removed by P0510.
>>>>
>>>
>>>
>>> Until this idea has been implemented and tested, this whole thread is a
>>> waste of time.
>>>
>>> Try to implement it, then come back with a real proposal with numbers to
>>> prove the claims.
>>>
>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>>

Received on 2026-04-19 09:28:13