C++ Logo

std-proposals

Advanced search

Re: [std-proposals] interceptor functions (tested and working on x86_64)

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Tue, 28 Apr 2026 12:16:16 +0200
Hi Frederick,

On 2026-04-28T11:06:40+0100, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Tuesday, April 28, 2026, Jonathan Wakely wrote:
>
> >
> >>>
> > The interceptor is declared as void, but it actually returns whatever the
> > 'goto' statement returns? Why not declare it as auto or decltype(auto)?
> >
>
>
>
> An interceptor function doesn't return. It jumps.

That's literally what a return statement is: it's a jump statement.
In fact, you'll find 'The return statement' specified as a subsection
under 'Jump statements'.

>
> Yes under the hood I have implemented it as two separate functions, a core
> and a thunk, and the core returns a value to the thunk, but that's all
> secretive under the hood stuff specific to my GNU patch. The guys at
> Microsoft or Embarcadero (formerly Borland) might just make it one
> function. I could have made it be just one function -- and actually I bet I
> could feed my entire compiler patch into ChatGPT and ask it to make that
> adjustment, and it might even get it right the first time or 2nd or 3rd
> time. I like the simplicity though of my implementation.

The translation to assembly/object code is unimportant.

> As far as the C++ programmer is concerned though, the inceptor function
> just jumps to another function -- and I thought the simplest way to express
> this would be to give it the signature "void Func(void)" and to use the
> syntax "goto ->".

As far as the abstract machine is concerned, the interceptor function
calls the intercepted function, as if it were just another regular
function, and returns as always.

> > This is horrible. Why would I ever want to write this? You have to name
> > std::printf inside the interceptor, *and* cast its type to std::printf when
> > you use it? That's horrible.
> >
> <snip>
>
> >
> > Why is this horribleness better than just wrapping std::printf in a lambda?
> >
> <snip>
>
>
> > What's the benefit of your interceptor?
> >
>
>
>
> I agree with you on all counts . . . a template lambda with a forwarding
> parameter pack is supreme over an interceptor function in every way --
> including aesthetic horridness and horribleness -- except in cases where
> you don't know (or don't care about) the signature of the target
> function... or if you want to write one interceptor which can be used with
> many different target function signatures.

Well, you could always do macro processing:

 #define foo(...) \
 ( \
  sleep(1), \
  foo(__VA_ARGS__) \
 )

This is completely type-agnostic and parameter-agnostic. It's more used
in C than in C++, but it's still a possibility if you need it.

>
> If you want to be agnostic to the target function signature, you must
> preserve the stack and all registers that might be used for passing
> arguments. That's exactly what my interceptor does.
>
> An interceptor will be very useful when dealing with pre-compiled
> proprietary libraries (for which you don't have the source code or even a
> header file), and also for dealing with other languages such as Haskell.

Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>

Received on 2026-04-28 10:16:26