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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:09:39+00:00 2026-06-17T08:09:39+00:00

I’m trying to wrap std::packaged_task inside another class in order to be used together

  • 0

I’m trying to wrap std::packaged_task inside another class in order to be used together with a task scheduler.

At the moment I got it all working except std::future support. To get std::future support I figured out I need to use std::packaged_task for the get_future() function that it provides.

I’ve been trying whole day all sorts of ways to get this to work, but I seem to be unable to properly declare and initialise the packaged_task using the return value from a std::bind. I have tried to decipher the implementations of all the related libstdc++ functions such as std::async, std::future, std::thread etc but with no luck.

The following code is the implementation of both the not working version and the working one. To get it to work uncomment the two /* — WORKS*/ and comment the other related line.

#include <vector>
#include <deque>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <iostream>
#include <chrono>
#include <functional>

#include <windows.h>

class task
{
private:
    struct task_implementation_base
    {
        virtual void execute() = 0;
    };

    template <class callable>
    struct task_implementation : public task_implementation_base
    {
        task_implementation(callable&& f) : /*m_task(std::forward<callable>(f)) WORKS*/m_task(f) { }
        void execute() { m_task(); }

        //callable m_task; // WORKS
        std::packaged_task<typename result_of<callable>::type> m_task;
    };

    template <class callable>
    std::shared_ptr<task_implementation<callable>> make_routine(callable&& f)
    {
        return std::make_shared<task_implementation<callable>>(std::forward<callable>(f));
    }

public:
    template <class callable, class... arguments>
    task(callable&& f, arguments&&... args) : m_function(make_routine(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...))) {}

    void operator()() { run(); }

    void run() { m_function->execute(); }

private:
    std::shared_ptr<task_implementation_base> m_function;

};

int testint(int i)
{
    std::cout << "test6" << " :: ran from thread " << std::this_thread::get_id() << "\n";
    fflush(stdout);
    return i+100;
}

void test(const char* text)
{
    std::cout << text << " :: ran from thread " << std::this_thread::get_id() << "\n";
    fflush(stdout);
}

class testclass
{
public:
    void print1() { test("test3"); }
    void print2() { test("test4"); }
    void print3(const char* text) { test(text); }

};

int main()
{
    testclass testclass1;
    testclass* testclass2 = new testclass;

    task test1(test, "test1");
    task test2([]() { test("test2"); });
    task test3(&testclass::print1, &testclass1);
    task test4(&testclass::print2, &*testclass2);
    task test5(&testclass::print3, &*testclass2, "test5");
    task test6(&testint, 1);

    test1();
    test2();
    test3();
    test4();
    test5();
    test6();

    Sleep(2000);

    return 0;
}

I’m thinking the problem is typename result_of<callable>::type. I’m guessing it doesn’t properly evaluates to the return type of the callable function.

I’m using c++ (Built by MinGW-builds project) 4.8.0 20121225 (experimental) on a Windows 8 64bit. I’m suspecting the errors are irrelevant since I guess I’m just simply trying to get this work the wrong way but here is a pastebin for the errors anyway: errors

  • 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-17T08:09:40+00:00Added an answer on June 17, 2026 at 8:09 am

    std::packaged_task not only takes the result type of the invoked function as a template argument but also the types of the arguments you are passing to the to be invoked function.

    You can define them as follows:

    // somewhere
    int foo(bool, int);
    
    // somewhere else
    std::packaged_task<int(bool, int)> p(foo);
    

    To fix your code you need to add two empty parenthesis pairs. What I explained above also applies to std::result_of.

    std::packaged_task<typename std::result_of<callable()>::type()> m_task;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am confused How to use looping for Json response Array in another Array.
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

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.