std::packaged_task constructor

It seems that there's a difference between () and = constructor (ctor) here.
1
2
3
4
5
6
7
8
9
#include <functional>
#include <future>
int addNumbers(int a, int b) {
  return a + b;
}
int main(){
  // std::packaged_task<int()> pt = std::bind(addNumbers, 1,2);  //doesn't work, why?
  std::packaged_task<int()> pt(std::bind(addNumbers, 1,2)); 
}

I thought since pt was initialized with the std::function object returned by std::bind (correct me if it's actually not), = here should invoke the ctor for that std::function object. Why the first one doesn't work?
Last edited on
Line 7 performs copy-initialization while line 8 performs direct-initialization. Copy-initialization does not consider explicit constructors.

Cppreference lists the relevant constructor template:
template <class F> explicit packaged_task( F&& f );
https://en.cppreference.com/w/cpp/thread/packaged_task/packaged_task

Also, you cannot assume that std::bind returns a std::function.
Last edited on
Topic archived. No new replies allowed.