Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:52:55+00:00 2026-05-26T06:52:55+00:00

With hooking I mean the ability to non-intrusively override the behavior of a function.

  • 0

With “hooking” I mean the ability to non-intrusively override the behavior of a function. Some examples:

  • Print a log message before and/or after the function body.
  • Wrap the function body in a try catch body.
  • Measure duration of a function
  • etc…

I have seen different implementations in various programming languages and libraries:

  • Aspect Oriented Programming
  • JavaScript’s first class functions
  • OOP decorator pattern
  • WinAPI subclassing
  • Ruby’s method_missing
  • SWIG‘s %exception keyword which is meant to wrap all functions in a try/catch block can be (ab)used for the purpose of hooking

My questions are:

  • IMO this is such an incredibly useful feature that I wonder why it has never been implemented as a C++ language feature. Are there any reasons that prevent this from being made possible?
  • What are some recommended techniques or libraries to implement this in a C++ program?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:52:56+00:00Added an answer on May 26, 2026 at 6:52 am

    If you’re talking about causing a new method to be called before/after a function body, without changing the function body, you can base it on this, which uses a custom shared_ptr deleter to trigger the after-body function. It cannot be used for try/catch, since the before and after need to be separate functions using this technique.

    Also, the version below uses shared_ptr, but with C++11 you should be able to use unique_ptr to get the same effect without the cost of creating and destroying a shared pointer every time you use it.

    #include <iostream>
    #include <boost/chrono/chrono.hpp>
    #include <boost/chrono/system_clocks.hpp>
    #include <boost/shared_ptr.hpp>
    
    template <typename T, typename Derived>
    class base_wrapper
    {
    protected:
      typedef T wrapped_type;
    
      Derived* self() {
        return static_cast<Derived*>(this);
      }
    
      wrapped_type* p;
    
      struct suffix_wrapper
      {
        Derived* d;
        suffix_wrapper(Derived* d): d(d) {};
        void operator()(wrapped_type* p)
        {
          d->suffix(p);
        }
      };
    public:
      explicit base_wrapper(wrapped_type* p) :  p(p) {};
    
    
      void prefix(wrapped_type* p) {
         // Default does nothing
      };
    
      void suffix(wrapped_type* p) {
         // Default does nothing
      }
    
      boost::shared_ptr<wrapped_type> operator->() 
      {
        self()->prefix(p);
        return boost::shared_ptr<wrapped_type>(p,suffix_wrapper(self()));
      }
    };
    
    
    
    
    template<typename T>
    class timing_wrapper : public base_wrapper< T, timing_wrapper<T> >
    {
      typedef  base_wrapper< T, timing_wrapper<T> > base;
      typedef boost::chrono::time_point<boost::chrono::system_clock, boost::chrono::duration<double> > time_point;
    
      time_point begin;
    public:
      timing_wrapper(T* p): base(p) {}
    
    
      void prefix(T* p) 
      {
        begin = boost::chrono::system_clock::now();
      }
    
      void suffix(T* p)
      {
        time_point end = boost::chrono::system_clock::now();
    
        std::cout << "Time: " << (end-begin).count() << std::endl;
      }
    };
    
    template <typename T>
    class logging_wrapper : public base_wrapper< T, logging_wrapper<T> >
    {
      typedef  base_wrapper< T, logging_wrapper<T> > base;
    public:
      logging_wrapper(T* p): base(p) {}
    
      void prefix(T* p) 
      {
        std::cout << "entering" << std::endl;
      }
    
      void suffix(T* p) 
      {
        std::cout << "exiting" << std::endl;
      }
    
    };
    
    
    template <template <typename> class wrapper, typename T> 
    wrapper<T> make_wrapper(T* p) 
    {
      return wrapper<T>(p);
    }
    
    
    class X 
    {
    public:
      void f()  const
      {
        sleep(1);
      }
    
      void g() const
      {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
      }
    
    };
    
    
    
    int main () {
    
      X x1;
    
    
      make_wrapper<timing_wrapper>(&x1)->f();
    
      make_wrapper<logging_wrapper>(&x1)->g();
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following directory structure : /script/x.js /includes/x.txt /1/2/index.html After hooking a webpage
Am having some trouble hooking up instance variables in the visual object editor using
I am examining the private c++ function hooking code snippet from mobilesubstrate and see
Using Chrome's developer tools I am trying to determine what jQuery function is hooking
I am hooking the Security event log with System.Diagnostics.Eventing.Reader.EventLogWatcher class, and I am watching
I'm trying to write some simple test code as a demonstration of hooking the
I need to perform some action when my application receives focus. I've tried hooking
I tried to run flyway in my application before hibernate is hooking in on
Does anyone have experience in hooking onto events. so we can modify there behavior
I've been looking at some hooking code which selectively loads a library into certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.