Reputation: 51489
I'm trying to return a custom type from a Concurrency Runtime task. My custom type should be constructible through a static factory method only (aside from being move-constructible), e.g.:
#include <utility>
struct foo
{
foo() = delete;
foo(foo const&) noexcept = delete;
foo& operator=(foo const&) noexcept = delete;
foo(foo&&) noexcept = default;
foo& operator=(foo&&) noexcept = default;
static foo make_foo(int const value) noexcept { return std::move(foo{ value }); }
private:
foo(int const) noexcept {}
};
And a simple test case:
#include <ppltasks.h>
using namespace concurrency;
int main()
{
auto&& task
{
create_task([value = 42]()
{
return foo::make_foo(value);
})
};
auto&& result{ task.get() };
}
This, however, fails to compile, producing the following (abridged) compiler diagnostic:
ppltasks.h(644,1): error C2280: 'foo::foo(const foo &) noexcept': attempting to reference a deleted function main.cpp(223): message : see declaration of 'foo::foo' main.cpp(223,5): message : 'foo::foo(const foo &) noexcept': function was explicitly deleted
No surprises here, the copy-c'tor is indeed explicitly deleted, on purpose. I just don't understand, why the move-constructor isn't considered as a candidate.
It seems that I can get the code to compile, as long as I provide a default-c'tor, a copy-c'tor, and a copy-assignment operator. Is this a limitation of the library (ConcRT), the compiler (Visual Studio 2019 16.1.0), the programming language, or my control over it?
Phrased another way: Is there anything I can do to get my custom type (as it is) to play along with concurrency::task
, or are default-constructibility, copy-constructibility, and copy-assignability undocumented requirements?
Upvotes: 1
Views: 122