C++ Logo

std-proposals

Advanced search

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

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Thu, 2 Apr 2026 23:57:29 +0200
Benchmarks do lie...

The first rule of benchmarking is not testing real code and this
creates biases that screw
results, considering simple things like CPU cache. Benchmark can
easily fill it full and run at 100% speed.
but in real code the same logic has only 0.1% cache available to it as
other code needs the same cache too.
If you have cache miss, the whole code run 100 times slower.

Only test final optimized assembly are true test of permanence,
benchmarks are only hits for searching optimal solutions.


And for your example, why not simply use get function pointer like
`&A::f` and that call this function directly?
Why bother with indexes?

```
#include <iostream>
#include <tuple>
#include <functional>

struct A { int get() { return 10; } };
struct B { double get() { return 1; } };
struct C { float get() { return 2.1; } };

template<typename C>
struct GetCallerArg;
template<typename T, typename R>
struct GetCallerArg<R (T::*)()>
{
    using type = T;
};

template<typename T, auto Callback>
void warper(T& a)
{
    std::cout<< std::invoke(
        Callback,
        std::get<typename GetCallerArg<decltype(Callback)>::type>(a)
    ) << '\n';
}
template<typename T>
using Access = void(*)(T&);



int main()
{
    std::tuple<A,B,C> t{ A{}, B{}, C{} };

    Access<decltype(t)> f = &warper<decltype(t), &A::get>;

    f(t);

    f = &warper<decltype(t), &B::get>;

    f(t);
}
```
https://godbolt.org/z/Y5fGEMdc4

Intresing compiler veven remove all warpers and only emit calls to `std::cout`

czw., 2 kwi 2026 o 23:20 Muneem via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> benchmarks don't lie! Even if the assembly is the same size and looks similar, benchmarks show otherwise. Do benchmarking in the code that I showed using my proposal.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-04-02 21:57:44