user2286810
user2286810

Reputation: 146

error C2248: 'std::promise<_Ty>::promise' : cannot access private member declared in class 'std::promise<_Ty>'

How can I correct the code below in order to avoid the error message? Is this code not supposed to work? Is there a different technique (packaged_task, asynch) that can be used?

#include <list>
#include <future>

using namespace std;

template<typename T>
struct sorter
{
    struct chunk_to_sort
    {
        std::list<T> data_m;
        std::promise<std::list<T> > promise_m;
    };
    list<chunk_to_sort> chunks_m;
    void do_sort(std::list<T>& chunk_data)
    {
        std::list<T> result;
        result.splice(result.begin(),chunk_data,chunk_data.begin());
        T const& partition_val=*result.begin();
        typename std::list<T>::iterator divide_point = std::partition(chunk_data.begin(),chunk_data.end(),[&](T const& val){return val<partition_val;});
        chunk_to_sort new_lower_chunk;
        new_lower_chunk.data_m.splice(new_lower_chunk.data_m.end(), chunk_data,chunk_data.begin(),divide_point);
        chunks_m.emplace_back(std::move(new_lower_chunk));
    }
};

int main()
{
    sorter<int> s;
    list<int> l;
    l.push_back(4);
    l.push_back(3);
    s.do_sort(l);
    return 0;
}

Upvotes: 3

Views: 529

Answers (1)

Praetorian
Praetorian

Reputation: 109109

Your code compiles successfully on both gcc and clang (once the missing <algorithm> header is included). It fails on MSVC because of this line

chunks_m.emplace_back(std::move(new_lower_chunk));

The problem is that VS2013 and earlier do not implicitly generate a move constructor, so the emplace_back call ends up trying to copy construct a chunk_to_sort which is ill-formed because std::promise is move-only.

To fix the problem provide a move constructor definition (and maybe a move assignment operator definition too).

chunk_to_sort() = default;

chunk_to_sort(chunk_to_sort&& other)
: data_m(std::move(other.data_m))
, promise_m(std::move(other.promise_m))
{}

Upvotes: 6

Related Questions