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

The Archive Base Latest Questions

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

I was experimenting with std::async and ended up with a code that looks like

  • 0

I was experimenting with std::async and ended up with a code that looks like that :

class obj {
public:
    int val;

    obj(int a) : val(a) {
        cout << "new obj" << endl;
    }
    ~obj() {
        cout << "delete obj" << endl;
    }
};


void foo(obj a) {

    this_thread::sleep_for(chrono::milliseconds(500));
    cout << a.val << endl;
}

int main(int argc, int **args) {

    obj a(5);
    auto future = async(foo, a);
    future.wait();

    return 0;
}

and the result is :

new obj
delete obj
delete obj
delete obj
5
delete obj
delete obj
delete obj

I then tried to change void foo(obj a) by void foo(obj &a) :

new obj
delete obj
delete obj
delete obj
5
delete obj
delete obj

Why would 5 copies of my object be made for this simple code?
I have to admit, I’m really confused. Would someone care to explain this?

Edit

I’m using VS2012

  • 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-18T23:17:33+00:00Added an answer on June 18, 2026 at 11:17 pm

    In your case, obj is being copied:

    1. Twice by the call to std::async.
    2. Twice by async‘s internal call to std::bind.
    3. Once by the call to void foo(obj a) since it takes a by value.

    Believe it or not, the number of copies has actually been reduced since VC10.

    It is not at all uncommon to see a library (be it the standard library or another one) trigger a few more copies than you would expect on your types. And usually, there is not too much you can do about it in.

    There are 2 things that people commonly do to prevent copies:

    1. Take obj by reference (or in your case, const ref since foo does not modify obj). This will require using std::ref with async.
    2. Define a move constructor for obj. This won’t prevent temporaries from being constructed and destroyed, but it will give you a chance to optimize the process a bit.

    Note that in your bare example of an object that holds onto only one int, it might actually be faster to copy rather than move or pass by reference.


    Example for passing obj by reference into async:

    void foo(const obj& a) {
        this_thread::sleep_for(chrono::milliseconds(500));
        cout << a.val << endl;
    }
    
    int main(int argc, int **args) {
        obj a(5);
        auto future = async(foo, std::cref(a));
        future.wait();
    
        return 0;
    }
    

    Example for defining a move constructor:

    class obj
    {
    public:
        /* ... */
    
        obj(obj&& a) : val(move(a.val)) {
            // It is good practice to 0 out the moved object to catch use-after-move bugs sooner.
            a.val = 0;
        }
    
        /* ... */
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While experimenting with C++11 new features, I discovered that the std::placeholders::_1 can't be directly
Here is some representative code that gets the error I'm experiencing: class Data {
I'm a Java developer experimenting with C++. I just created a new class. In
I've been experimenting with std::tuple in combination with references: #include <iostream> #include <tuple> int
I was experimenting with C++ and found the below code as very strange. class
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
I am experimenting with Dart. I have created an on click event like so:
While experimenting a bit with C++ templates I managed to produce this simple code,
I am new to C++ and I am experimenting with the dirent.h header to
I'm experimenting with std::remove_reference. For example, I can extract an element type an array

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.