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 700661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:31:23+00:00 2026-05-14T03:31:23+00:00

I’m writing some callback implementation in C++. I have an abstract callback class, let’s

  • 0

I’m writing some callback implementation in C++.

I have an abstract callback class, let’s say:

/** Abstract callback class. */
class callback {
public:

    /** Executes the callback. */
    void call() { do_call(); };
protected:

    /** Callback call implementation specific to derived callback. */
    virtual void do_call() = 0;
};

Each callback I create (accepting single-argument functions, double-argument functions…) is created as a mixin using one of the following:

/** Makes the callback a single-argument callback. */
template <typename T>
class singleArgumentCallback {
protected:
    /** Callback argument. */
    T arg;

public:
    /** Constructor. */
    singleArgumentCallback(T arg): arg(arg) { }
};

/** Makes the callback a double-argument callback. */
template <typename T, typename V>
class doubleArgumentCallback {
protected:
    /** Callback argument 1. */
    T arg1;

    /** Callback argument 2. */
    V arg2;

public:
    /** Constructor. */
    doubleArgumentCallback(T arg1, V arg2): arg1(arg1), arg2(arg2) { }
};

For example, a single-arg function callback would look like this:

/** Single-arg callbacks. */
template <typename T>
class singleArgFunctionCallback:
    public callback,
    protected singleArgumentCallback<T> {

    /** Callback. */
    void (*callbackMethod)(T arg);

public:
   /** Constructor. */
    singleArgFunctionCallback(void (*callback)(T), T argument):
        singleArgumentCallback<T>(argument),
        callbackMethod(callback) { }

protected:
    void do_call() {
        this->callbackMethod(this->arg);
    }
};

For user convenience, I’d like to have a method that creates a callback without having the user think about details, so that one can call (this interface is not subject to change, unfortunately):

void test3(float x) { std::cout << x << std::endl; }
void test5(const std::string& s) { std::cout << s << std::endl; }

make_callback(&test3, 12.0f)->call();
make_callback(&test5, "oh hai!")->call();

My current implementation of make_callback(...) is as follows:

/** Creates a callback object. */
template <typename T, typename U> callback* make_callback(
    void (*callbackMethod)(T), U argument) {
    return new singleArgFunctionCallback<T>(callbackMethod, argument);
}

Unfortunately, when I call make_callback(&test5, "oh hai!")->call(); I get an empty string on the standard output. I believe the problem is that the reference gets out of scope after callback initialization.

I tried using pointers and references, but it’s impossible to have a pointer/reference to reference, so I failed. The only solution I had was to forbid substituting reference type as T (for example, T cannot be std::string&) but that’s a sad solution since I have to create another singleArgCallbackAcceptingReference class accepting a function pointer with following signature:

void (*callbackMethod)(T& arg);

thus, my code gets duplicated 2^n times, where n is the number of arguments of a callback function.

Does anybody know any workaround or has any idea how to fix it? Thanks in advance!

  • 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-14T03:31:24+00:00Added an answer on May 14, 2026 at 3:31 am

    The problem is that in make_callback(), T becomes const std::string&, which in turn becomes T in your singleArgumentCallback. U, however, is a const char*, so a temporary std::string object is created and bound to that reference in singleArgumentCallback. When make_callback() finishes, that temporary is destroyed, leaving the created singleArgumentCallback object with a reference to a no longer existing object.

    What you will have to do is to first remove references (and, maybe, cv qualifiers) from the types passed into make_callback(). As Marcelo suggested, Boost.TypeTraits can help you do this, but if you want, it isn’t hard to cook up something on your own:

    template< typename T > struct remove_ref     { typedef T result_type; };
    template< typename T > struct remove_ref<T&> { typedef T result_type; };
    

    Then change make_callback() to:

    template <typename T, typename U>
    callback* make_callback(void (*callbackMethod)(T), U argument)
    {
        typedef typename remove_ref<T>::result_type  arg_type;
        return new singleArgFunctionCallback<arg_type>(callbackMethod, argument);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer UITabBarController provides a delegate method called tabBarController:shouldSelectViewController:. Override that and… May 14, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer There are no "officially supported mimetypes for Android" except to… May 14, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer I would do it as a view. This is basically… May 14, 2026 at 9:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.