C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Interceptor Function

From: Zhao YunShan <dou1984_at_[hidden]>
Date: Tue, 14 Apr 2026 21:31:22 +0800 (CST)
First, replacing function symbol at link time is a proven approach; g++ --wrap has been doing this for years. Promoting this compiler flag feature to a native language feature would enhance code readability and make it effortless for developers to apply. Why not do it?


Second, it would be even better if the compiler could handle function symbol replacement directly.


For example:


template <class Func, class ...Args>
Func __wrap(Func __real_func)
{
    return [__real_func](Args&&... args) -> decltype(__real_func(args...))
    {
        do_something(std::forward<Args>(args)...);
        return __real_func(std::forward<Args>(args)...);
    };
}
[[hook __wrap]]
int __real_send(int fd, const void *buf, size_t len, int flags)
{
    return send(fd, buf, len, flags);
}
กฐ[[hook __wrap]]" is equivalent to:
auto __wrap_real_send = __wrap(__real_send);


During compilation, the compiler would first specialize __wrap(__real_send) into __wrap_real_send, then replace references to __real_send with __wrap_real_send, and finally perform optimizations. I believe this is a trivial task for a compiler.
We can discuss the feasibility of the approach first, and the syntax later. If the approach works, the specific syntax is secondary.




>Date: Tue, 14 Apr 2026 13:52:18 +0200
>From: Sebastian Wittmeier <wittmeier_at_[hidden]>
>To: std-proposals_at_lists.isocpp.org <std-proposals_at_[hidden]>
>Subject: Re: [std-proposals] Interceptor Function (preserve stack and
> all registers)
>Message-ID:
> <zarafa.69de2a72.4130.26d52b5c5bc0ef51_at_lvps176-28-11-36.dedicated.hosteurope.>
>
>Content-Type: text/plain; charset="utf-8"
>
>If the compiler processes it, there is no need for you for providing assembler. Or for defining how the implementations do it.
>
>It is just compiled or even optimized like normal C++ code.
>
>?
>You can (with a C++ standard proposal) just declare two template functions (to be flexible about the parameters/overload), which are called before a call to the marked function and after a call to the marked function. (The question is, whether the marking may be attributes, as it changes the program)
>
>?
>Then you
>
>?- get notified about the call,
>
>?- can set a debug breakpoint (if you have some location, which is not optimized away)
>
>?- can log the parameters
>
>?- get notified about the return and return type
>
>?
>?
>Something like _penter, but inserted on the call site.
>
>https://learn.microsoft.com/en-us/cpp/build/reference/gh-enable-penter-hook-function
>
>
>?
******************

Received on 2026-04-14 13:31:31