Why is std::packaged_task not valid?

前端 未结 3 1837
北荒
北荒 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:25

    The problem is still there in MSVS 2013RC, but I made this temporary patch while MS corrects it. It is a specialization of packaged_task for void(...), so I suggest putting this in a header file and including it after the standard headers.Note that make_ready_at_thread_exit() is not implemented and some functions have not been fully tested, use at your own risk.

    namespace std {
    
    template
    class packaged_task
    {
        promise _my_promise;
        function _my_func;
    
    public:
        packaged_task() {
        }
    
        template
        explicit packaged_task(_Fty2&& _Fnarg)
          : _my_func(_Fnarg) {
        }
    
        packaged_task(packaged_task&& _Other)
          : _my_promise(move(_Other._my_promise)),
            _my_func(move(_Other._my_func)) {
        }
    
        packaged_task& operator=(packaged_task&& _Other) {
            _my_promise = move(_Other._my_promise);
            _my_func = move(_Other._my_func);
            return (*this);
        }
    
        packaged_task(const packaged_task&) = delete;
        packaged_task& operator=(const packaged_task&) = delete;
    
        ~packaged_task() {
        }
    
        void swap(packaged_task& _Other) {
            _my_promise.swap(_Other._my_promise);
            _my_func.swap(_Other._my_func);
        }
    
        explicit operator bool() const {
            return _my_func != false;
        }
    
        bool valid() const {
            return _my_func != false;
        }
    
        future get_future() {
            return _my_promise.get_future();
        }
    
        void operator()(_ArgTypes... _Args) {
            _my_func(forward<_ArgTypes>(_Args)...);
            _my_promise.set_value();
        }
    
        void reset() {
            swap(packaged_task());
        }
    };
    
    }; // namespace std
    

提交回复
热议问题