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

  • Home
  • SEARCH
  • 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 8858465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:50:07+00:00 2026-06-14T14:50:07+00:00

I have written the following wrapper for std::bind and std::queue : #include Queue.h template<class

  • 0

I have written the following wrapper for std::bind and std::queue:

#include "Queue.h"

template<class T>
Queue<T>::Queue(T* input)
{
    instance = input;
}

template<class T> template<typename... Args>
int Queue<T>::push(int (T::*func)(Args... args), Args... args)
{
    queue.push(std::bind(func, instance, args...));
    return queue.size();
}

template<class T>
int Queue<T>::pop()
{
    if(!queue.empty())
    {
        queue.front()();
        queue.pop();
        return queue.size();
    }
    return 0;
}

template<class T>
bool Queue<T>::empty()
{
    return queue.empty();
}

template<class T>
size_t Queue<T>::size()
{
    return queue.size();
}

with the following header:

#ifndef QUEUE_H_
#define QUEUE_H_

#include <functional>
#include <queue>

template <class T>
class Queue
{
    private:
        std::queue<std::function<void()>> queue;    /**< the messaging queue, appended to using enqueue(), popped from using dequeue() */
        T* instance;

    public:

        Queue(T*);

        template<typename... Args>
        int enqueue(int (T::*f)(Args... args), Args... args);

        int dequeue();

        bool empty();

        size_t size();
};

#endif

It allows me to add bound function expressions to a queue and pop them afterwards (queue->push<int>(&Object::jumpAround, 10); and queue->pop()). The problem is, I could not find a generic object- and function-pointer that enabled me to implement this without the <class T> template.

I know that using templates would probably be the safest and best approach here but due to the design of the code implementing this queue I need to get rid of it. Any ideas?

I guess it must be possible somehow because std::bind‘s first parameter can be any function and the second one can be any Object.

  • 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-06-14T14:50:08+00:00Added an answer on June 14, 2026 at 2:50 pm

    If I understand, below is what you require:

    class Queue
    {
    private:
      std::queue<std::function<void()>> queue;    /**< the messaging queue, appended to using enqueue(), popped from using dequeue() */
    public:
    
      // pass in the instance of the object and simply allow the compiler to deduce the function pointer nastiness...
      template<typename T, typename F, typename... Args>
      void enqueue(T instance, F func, Args... args)
      {
        queue.push(std::bind(func, instance, args...));
      }
    
      int dequeue()
      {
        if(!queue.empty())
        {
          queue.front()();
          queue.pop();
        }
      }
    };
    

    Oh and how to use it:

    struct foo
    {
      void bar(int a)
      {
        std::cout << "foo::bar: " << a << std::endl;
      }
    };
    
    struct bar
    {
      void foo(int a, int c)
      {
        std::cout << "bar::foo: " << (a  + c)<< std::endl;
      }
    };
    
    int main(void)
    {
      Queue q;
      foo f;
      bar b;
      q.enqueue(&f, &foo::bar, 10);
      q.enqueue(&b, &bar::foo, 10, 11);
    
      q.dequeue();
      q.dequeue();
    }
    

    Should output:

    foo::bar: 10
    bar::foo: 21
    

    Or, even better, change your function signature and allow users to enqueue a std::function! This is the “normal” way (see for example, boost::asio::io_service::post.)

    EDIT: Here is a simple example:

    // Let the compiler do all the hard work for you..
    template<typename T>
    void enqueue(T f)
    {
      queue.push(f);
    }
    

    Now to post any function to this queue…

    // Here you are posting the functor itself...
    q.enqueue(std::bind(&bar::foo, &b, 15, 12));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the following code class Program { static void Main(string[] args) {
I have written the following Utility class to get an instance of any class
I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity
I have written following xaml code: <Window x:Class=WpfApplication3.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=200 Width=200> <StackPanel>
I have written a wrapper around the C# Facebook SDK usign the following code:
I have written following app.js class to get 3 tabs in my app using
I have written following code in html: <input type=text id=id_1 name=text_1> <input type=text id=id_2
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works

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.