mercredi 1 novembre 2017

use_count() of shared_ptr moved into a std::async in gcc 4.6.3

In the code below, I want the use_count() of the shared_ptr moved into the std::async to be 1:

#include <memory>
#include <iostream> 
#include <future> 

using namespace std;

void fun(shared_ptr<int> sp)
{
    cout << "fun: sp.use_count() == " << sp.use_count() <<
        " (in gcc 4.6.3, is there a way to make this 1?)\n";
}

int main()
{
    auto sp1 = make_shared<int>(5);

    auto fut = async(
        launch::async,
        fun,
        move(sp1)
    );
}


My platform uses gcc 4.6.3, and the code above gives this output (fun: sp.use_count() == 2):

fun: sp.use_count() == 2 (in gcc 4.6.3, is there a way to make this 1?)


On coliru.stacked-crooked.com, I get the behavior that I want (fun: sp.use_count() == 1):

fun: sp.use_count() == 1 (in gcc 4.6.3, is there a way to make this 1?)


I'm not sure what compiler coliru is using, but I'm guessing it's newer than gcc 4.6.3.

Is there some way to get the behavior I want, without having to upgrade my compiler from gcc 4.6.3?

Aucun commentaire:

Enregistrer un commentaire