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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:32:25+00:00 2026-06-16T23:32:25+00:00

The code below is based on Herb Sutter’s ideas of an implementation of a

  • 0

The code below is based on Herb Sutter’s ideas of an implementation of a .then() type continuation.

  template<typename Fut, typename Work>
auto then(Fut f, Work w)->std::future<decltype(w(f.get()))>
  { return std::async([=] { w(f.get()); }); }

This would be used like auto next = then(f, [](int r) { go_and_use(r); }); or similar.

This is a neat idea, but as it stands will not work (futures are move only and not copyable). I do like the idea as it is likely to appear in upcoming versions of c++ as far as I can guess (although as .then() or even await.)

Before making the futures shared or similar I wonder what the stack overflow community would think of this implementation specifically with improvements and suggestions (even shared futures)?

Thanks in advance for any suggestions.

(I am aware this is a fix till a standards based mechanism exists as it will cost a thread (maybe))).

  • 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-16T23:32:27+00:00Added an answer on June 16, 2026 at 11:32 pm

    I find 3 problems with the above implemention:

    • It will only work if you pass std::shared_future as Fut.
    • The continuation might want a chance to handle exceptions.
    • It will not always behave as expected, since if you do not specify std::launch::async it might be deferred, thus the continuation is not invoked as one would expect.

    I’ve tried to address these:

    template<typename F, typename W, typename R>
    struct helper
    {
        F f;
        W w;
    
        helper(F f, W w)
            : f(std::move(f))
            , w(std::move(w))
        {
        }
    
        helper(const helper& other)
            : f(other.f)
            , w(other.w)
        {
        }
    
        helper(helper&& other)
            : f(std::move(other.f))
            , w(std::move(other.w))
        {
        }
    
        helper& operator=(helper other)
        {
            f = std::move(other.f);
            w = std::move(other.w);
            return *this;
        }
    
        R operator()()
        {
            f.wait();
            return w(std::move(f)); 
        }
    };
    
    }
    
    template<typename F, typename W>
    auto then(F f, W w) -> std::future<decltype(w(F))>
    { 
        return std::async(std::launch::async, detail::helper<F, W, decltype(w(f))>(std::move(f), std::move(w))); 
    }
    

    Used like this:

    std::future<int> f = foo();
    
    auto f2 = then(std::move(f), [](std::future<int> f)
    {
        return f.get() * 2; 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to Herb Sutter the code below wouldn't compile. See this site http://www.gotw.ca/gotw/066.htm from
I am using the javascript code below to hide/how section based on project type
My code below counts tickets numbers and stores them into arrays based on their
I am using the script code below to get results from my database based
I have the below code, which iterates over a list based on a custom
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
The code below is supposed to initiate CSS based on if($row[datesubmitted] > $livetime){ .
In the code below, i can only filter based on the Surname column in
Code below defines a ChargeCustomer class that contains an array of type customers. I
Based on this code below I use for regular mysql, how could I convert

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.