Why is std::packaged_task not valid?

前端 未结 3 1815
北荒
北荒 2021-01-12 07:02

Using MSVC2012,

The following code will compile and run as expected

std::packaged_task< int() > task( []()->int{ std::cout << \"hello          


        
3条回答
  •  攒了一身酷
    2021-01-12 07:33

    This works in gcc 4.7.2:

    #include 
    #include 
    #include 
    
    int main() {
      std::packaged_task< void() > task( [](){ std::cout << "hello world" << std::endl; } );
      std::thread t( std::move(task) );
      t.join();
      std::packaged_task< int() > task2( []()->int{ std::cout << "hello world" << std::endl; return 0; } );
      std::thread t2( std::move(task2) );
      t2.join();
    }  
    

    Together with @WhozCraig 's archeology implies that this is probably a bug in MSVC2012.

    For a workaround, try using struct Nothing {}; or nullptr_t as your return value?

提交回复
热议问题