C++ Logo

std-proposals

Advanced search

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

From: Muneem <itfllow123_at_[hidden]>
Date: Thu, 2 Apr 2026 09:56:44 +0500
//here is another one:
#include <variant>
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
struct A { int get() { return 1; } };
struct B { int get() { return 2; } };
struct C { int get() { return 10; } };

struct data_accessed_through_visit {
    std::variant<A, B, C> obj=C{};

    int operator()(int) {
        return std::visit([](auto&& arg) {
            return arg.get();
        }, obj);
    }
};

std::variant<A, B, C> data_accessed_through_visit::obj = C{};
int a_val = 1, b_val = 2, c_val = 10;
int user_index = 0;

struct data_ternary {
    int operator()(int index) {
        return (index == 0) ? a_val : (index == 1) ? b_val : (index == 2) ?
c_val : -1;
    }
};

struct data_switched {
    int operator()(int index) {
        switch(index) {
            case 0: return a_val;
            case 1: return b_val;
            case 2: return c_val;
            default: return -1;
        }
    }
};





volatile int x = 0;
constexpr uint64_t loop_count=100000;
static void measure_switch() {
    data_switched obj;
    for (int i=0; i++<loop_count;) {
        x = obj(user_index);
    }
}

static void measure_visit() {
    data_accessed_through_visit obj;
    for (int i=0; i++<loop_count;) {
        x = obj(user_index);
    }
}

static void measure_ternary() {
    data_ternary obj;
    for (int i=0; i++<loop_count;) {
        x = obj(user_index);
    }
}

template<typename func_t>
void call_func(func_t callable_obj, int arg){
    const auto start = std::chrono::steady_clock::now();
    callable_obj();
    const auto end = std::chrono::steady_clock::now();

    std::cout<<
    std::chrono::duration_cast<std::chrono::nanoseconds>(end -
start).count()
    <<std::endl;

}

int main() {
    std::cout << "Enter index (0 for A, 1 for B, 2 for C): ";
    if (!(std::cin >> user_index)) return 1;

    // Set the variant state
    if (user_index == 0) data_accessed_through_visit::obj = A{};
    else if (user_index == 1) data_accessed_through_visit::obj = B{};
    else if (user_index == 2) data_accessed_through_visit::obj = C{};

    std::cout << "Time (ns) for switch: ";
    call_func(measure_switch, user_index);

    std::cout << "Time (ns) for visit: ";
    call_func(measure_visit, user_index);

    std::cout << "Time (ns) for ternary: ";
    call_func(measure_ternary, user_index);

    return 0;
}
/*
Time (ns) for switch: 168100
Time (ns) for visit: 3664100
Time (ns) for ternary: 190900
It keeps on getting worse!
*/



On Thu, Apr 2, 2026 at 9:48 AM Muneem <itfllow123_at_[hidden]> wrote:

> Use the index 2 while benchmarking, or else the std::visit will fail,
> which again proves my point on the fact that "std::visit is just a dumb
> union"
>
> regards, Muneem
>
> On Thu, Apr 2, 2026 at 9:46 AM Muneem <itfllow123_at_[hidden]> wrote:
>
>> basically, the turn of events were these:
>> dealing with the errors in https://quick-bench.com/ was painful because
>> it wasn't accepting user input when i debugged it all, then when i complete
>> the program, I didn't have a compiler, then had a emotional fight with my
>> mom, then tried installing a compiler, failed at first, installed an old
>> one at the second, then at last, it worked. Sorry for trauma dumping, but I
>> just cant hold it back when I am missing on time with the math book that I
>> love so dearly(my routine didn't turn out the way it was supposed to), and
>> during all that I got an email from a guy from the standard(according to
>> linkdin) calling my idea stupid and to not continue it; Life really had to
>> give me this meltdown. Sorry for the trauma dump story. Ignore it, please
>> (for me)
>>
>> On Thu, Apr 2, 2026 at 9:45 AM Muneem <itfllow123_at_[hidden]> wrote:
>>
>>> Hi!
>>> //sorry for my late bench mark results; I had a very unfortunate turn of
>>> events in the past few hours. REALLY REALLY SORRY!!!!
>>> #include <variant>!!
>>> #include <iostream>
>>> #include <chrono>
>>> #include <ctime>
>>> #include <iomanip>
>>> struct A { inline int get(){return 1;} };
>>> struct B { inline int get(){return 2;} };
>>> struct C { inline int get(){return 3;} };
>>>
>>> struct data_accessed_through_visit {
>>> static std::variant<A, B, C> obj;
>>> int operator()(int) {
>>> int a1;
>>> std::visit([&a1](auto&& arg) { a1= arg.get(); }, obj);
>>> return a1;
>>> }
>>> };
>>>
>>> std::variant<A, B, C> data_accessed_through_visit::obj = C{};
>>> int a_val = 1, b_val = 2, c_val = 10;
>>> int user_index = 0;
>>>
>>> struct data_ternary {
>>> int operator()(int index) {
>>> return (index == 0) ? a_val : (index == 1) ? b_val : (index ==
>>> 2) ? c_val : -1;
>>> }
>>> };
>>>
>>> struct data_switched {
>>> int operator()(int index) {
>>> switch(index) {
>>> case 0: return a_val;
>>> case 1: return b_val;
>>> case 2: return c_val;
>>> default: return -1;
>>> }
>>> }
>>> };
>>>
>>>
>>>
>>>
>>>
>>> volatile int x = 0;
>>> constexpr uint64_t loop_count=100000;
>>> static void measure_switch() {
>>> data_switched obj;
>>> for (int i=0; i++<loop_count;) {
>>> x = obj(user_index);
>>> }
>>> }
>>>
>>> static void measure_visit() {
>>> data_accessed_through_visit obj;
>>> for (int i=0; i++<loop_count;) {
>>> x = obj(user_index);
>>> }
>>> }
>>>
>>> static void measure_ternary() {
>>> data_ternary obj;
>>> for (int i=0; i++<loop_count;) {
>>> x = obj(user_index);
>>> }
>>> }
>>>
>>> template<typename func_t>
>>> void call_func(func_t callable_obj, int arg){
>>> const auto start = std::chrono::steady_clock::now();
>>> callable_obj();
>>> const auto end = std::chrono::steady_clock::now();
>>>
>>> std::cout<<
>>> std::chrono::duration_cast<std::chrono::nanoseconds>(end -
>>> start).count()
>>> <<std::endl;
>>>
>>> }
>>>
>>> int main(){
>>> std::cout << "Enter index (0 for A, 1 for B, 2 for C): ";
>>> if (!(std::cin >> user_index)) std::cerr<<"error!";
>>> if (user_index == 0) data_accessed_through_visit::obj = A{};
>>> else if (user_index == 1) data_accessed_through_visit::obj = B{};
>>> else if (user_index == 2) data_accessed_through_visit::obj = C{};
>>> std::cout<<"time taken for switch statements:";
>>> measure_switch();
>>> std::cout<<"time taken for visit:";
>>> measure_visit();
>>> std::cout<<"time taken for ternary:";
>>>
>>> measure_ternary();
>>> return 0;
>>>
>>> }
>>> /*
>>> results:
>>> Time (ns) for switch: 442900
>>> Time (ns) for visit: 3682600
>>> Time (ns) for ternary: 248300
>>> Told you!!!
>>> thank you for all of you're feedbacks!!!
>>> */
>>> regards, Muneem
>>>
>>>
>>> On Thu, Apr 2, 2026 at 8:26 AM Muneem <itfllow123_at_[hidden]> wrote:
>>>
>>>> The benchmarking tools that you (Breno Guimarães )
>>>> suggested didn't work because they do compile time optimizations on
>>>> the indexing itself (if the index can be determined at compile time), and
>>>> when I tried to make them read the index, it didn't work as expected, hence
>>>> it will take some time for me to write my own benchmarking code..
>>>> To the response I got from a very senior person, I wanna say that even
>>>> though this idea might not be within the logical framework of C++, I would
>>>> still push for it until there is a mathematical proof to specify otherwise.
>>>> I hope this isnt disrespect. Sorry for the delay on benchmarks too, guys; I
>>>> was busy eating breakfast.
>>>>
>>>> On Thu, Apr 2, 2026 at 8:24 AM Muneem <itfllow123_at_[hidden]> wrote:
>>>>
>>>>> Before I add some code, I wanna specify that I know references are the
>>>>> same as pointers,but compilers can (in theory optimize them better because
>>>>> they are like constant pointers).
>>>>>
>>>>> On Thu, Apr 2, 2026 at 6:35 AM Breno Guimarães <brenorg_at_[hidden]>
>>>>> wrote:
>>>>>
>>>>>> Try adding actual code showing the problem and how it looks like with
>>>>>> the different options you claim de be not sufficient and how the code would
>>>>>> look like with your solutions.
>>>>>>
>>>>>> There are many wrong assumptions on how things work today. I honestly
>>>>>> cannot understand what you are actually proposing.
>>>>>>
>>>>>> Em qua., 1 de abr. de 2026, 20:37, Muneem via Std-Proposals <
>>>>>> std-proposals_at_[hidden]> escreveu:
>>>>>>
>>>>>>> This is the updated proposal
>>>>>>>
>>>>>>> On Thu, Apr 2, 2026 at 1:45 AM Muneem <itfllow123_at_[hidden]> wrote:
>>>>>>>
>>>>>>>> Ok, I will draft a format proposal that outline all the problems (a
>>>>>>>> whole class of problems), cite all the possible solutions, and then cite
>>>>>>>> bjarne Stroustrup quotes from his old 2013 book to back my philosophy up.
>>>>>>>> Give me some time and thank you for your feedback ❤️❤️
>>>>>>>>
>>>>>>>> Regards, Muneem
>>>>>>>>
>>>>>>>> On Thu, 2 Apr 2026, 1:15 am Thiago Macieira via Std-Proposals, <
>>>>>>>> std-proposals_at_[hidden]> wrote:
>>>>>>>>
>>>>>>>>> On Wednesday, 1 April 2026 10:52:08 Pacific Daylight Time Muneem
>>>>>>>>> via Std-
>>>>>>>>> Proposals wrote:
>>>>>>>>> > Okay, I won't make the JIT part in my proposal, but just so I
>>>>>>>>> know, you do
>>>>>>>>> > support me if I dont? Like you will vouch for me and help me in
>>>>>>>>> my actual
>>>>>>>>> > formal proposal?
>>>>>>>>>
>>>>>>>>> I haven't understood the problem yet. You need to start the
>>>>>>>>> proposal with the
>>>>>>>>> problems (plural) that need solving, preferably with multiple
>>>>>>>>> possible
>>>>>>>>> alternatives and showing why you think your solution is best.
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>>>>>>>>> Principal Engineer - Intel Data Center - Platform & Sys. Eng.
>>>>>>>>> --
>>>>>>>>> Std-Proposals mailing list
>>>>>>>>> Std-Proposals_at_[hidden]
>>>>>>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>>>>>>
>>>>>>>> --
>>>>>>> Std-Proposals mailing list
>>>>>>> Std-Proposals_at_[hidden]
>>>>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>>>>
>>>>>>

Received on 2026-04-02 04:57:01