C++ Logo

std-proposals

Advanced search

[std-proposals] Interceptor Function (preserve stack and all registers)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 14 Apr 2026 15:28:04 +0100
On Monday, April 13, 2026, Thiago Macieira wrote:

>
>
> Because there's no use-case possible for it that would apply in the
> Standard's
> abstract machine. You *cannot* replace a function.
>
>
>

The Standard could describe interceptors by using the following example
code:


int FuncA(int const arg) { return arg + 1; }
int FuncB(int const arg) { return arg + 2; }
int FuncC(int const arg) { return arg + 3; }

[[interceptor]] void Monkey(void)
{
    static unsigned counter = -1;
    ++counter;
    switch ( counter % 3u )
    {
    case 0: goto -> FuncA;
    case 1: goto -> FuncB;
    case 2: goto -> FuncC;
    }
}

#include <iostream>

int main(void)
{
    constexpr auto fp = (int(*)(int))&Monkey;

    using std::cout, std::endl;

    cout << fp(92) << fp(93) << fp(94) << endl;
}


The above program will print out:


939597


I don't see why the Standard's abstract machine prevents the above.

Received on 2026-04-14 14:28:08