visual studio implementation of “move semantics” and “rvalue reference”

后端 未结 2 446
终归单人心
终归单人心 2020-12-10 19:14

I came across a Youtube video on c++11 concurrency (part 3) and the following code, which compiles and generates correct result in the video.

However, I got a compi

2条回答
  •  自闭症患者
    2020-12-10 19:53

    What is returned from std::move is indeed an rvalue reference, but that doesn't matter because the thread constructor does not use perfect forwarding for its arguments. First it copies/moves them to storage owned by the new thread. Then, inside the new thread, the supplied function is called using the copies.

    Since the copies are not temporary objects, this step won't bind to rvalue-reference parameters.

    What the Standard says (30.3.1.2):

    The new thread of execution executes

    INVOKE( DECAY_COPY(std::forward(f)),  DECAY_COPY(std::forward(args))... )
    

    with the calls to DECAY_COPY being evaluated in the constructing thread.

    and

    In several places in this Clause the operation DECAY_COPY(x) is used. All such uses mean call the function decay_copy(x) and use the result, where decay_copy is defined as follows:

    template  decay_t decay_copy(T&& v)
    { return std::forward(v); }
    

    The value category is lost.

提交回复
热议问题