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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:10:24+00:00 2026-05-31T12:10:24+00:00

I need a function wrapper for std::bind that will be called before the function

  • 0

I need a function wrapper for std::bind that will be called before the function it’s wrapper, passing the arguments along to the wrapped functions.

std::function<void (int)> foo = postbind<int>(service, handle);

I’ve so far got down to that

std::function<void (int)> foo = postbind(service, handle);

How can I remove that template parameter? It seems to come down to the type deduction from the object generation function (postbind) not being intelligent enough.

#include <functional>

template<typename T>
void foo(std::function<void (T)> func)
{
}

void handler(int x)
{
}

int main()
{
    foo(handler);
    return 0;
}

Says error: no matching function for call to ‘foo(void (&)(int))’

Yet, the code sample:

template<typename T>
void foo(T t)
{
}

int main()
{
    foo(99);
    return 0;
}

This works. Any ideas how to make this work? I need to be able to pass std::bind to it and have the result cast successfully to std::function.

How can I remove the template parameters? Thanks.


Q. What is service and this class meant to do?

A. Encapsulate a function wrapper that boost::asio::io_service->posts out of the current thread.


Full sourcecode:

#include <iostream>
#include <functional>
#include <memory>

class io_service
{
};

typedef std::shared_ptr<io_service> service_ptr;

template <typename Arg1>
class postbind_impl_1
{
public:
    typedef std::function<void (Arg1)> function;

    postbind_impl_1(service_ptr service, function memfunc)
      : service_(service), memfunc_(memfunc)
    {
    }

    void operator()(Arg1 arg1)
    {
        // do stuff using io_service
        memfunc_(arg1);
    }
private:
    service_ptr service_;
    function memfunc_;
};

template <typename Arg1>
postbind_impl_1<Arg1> postbind(service_ptr service, 
        typename postbind_impl_1<Arg1>::function handle)
{
    return postbind_impl_1<Arg1>(service, handle);
}

// ----------------

void handle(int x)
{
    std::cout << x << "\n";
}

int main()
{
    service_ptr service;
    std::function<void (int)> foo = postbind(service, handle);
    foo(110);
    return 0;
}
  • 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-31T12:10:25+00:00Added an answer on May 31, 2026 at 12:10 pm

    To all the naysayers who said this wasn’t possible 🙂

    /*
     * Defines a function decorator ala Python
     *
     *   void foo(int x, int y);
     *   function<void ()> wrapper(function<void (int)> f);
     *
     *   auto f = decorator(wrapper, bind(foo, 110, _1));
     *   f();
     */
    
    #include <functional>
    
    template <typename Wrapper, typename Functor>
    struct decorator_dispatch
    {
        Wrapper wrapper;
        Functor functor;
    
        template <typename... Args>
        auto operator()(Args&&... args)
            -> decltype(wrapper(functor)(std::forward<Args>(args)...))
        {
            return wrapper(functor)(std::forward<Args>(args)...);
        }
    };
    
    template <typename Wrapper, typename Functor>
    decorator_dispatch<
        Wrapper,
        typename std::decay<Functor>::type
    >
    decorator(Wrapper&& wrapper, Functor&& functor)
    {
        return {wrapper, functor};
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function to return a string that will only be accessed read-only.
I need a function that will convert a type to a string, for example:
I need a function that can return the difference between the below two dates
I need a function that returns the ASCII value of a character, including spaces,
I need a function that a user can just click a flash app button
I've written a C++ wrapper function for libxml2 that makes it easy for me
I have an exercise where I need to write a wrapper function for strcat.
I have a wrapper function used to send requests to a server, I need
I'm creating a wrapper function around mysqli so that my application needn't be overly
in function need key to encrypt string without mcrypt libraly in php function encrypt($str,

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.