Is there a cast (or standard function) with the opposite effect to `std::move`

前端 未结 4 1472
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 14:02

First off, this question is not a duplicate of Function dual to std::move? or of Does the inverse of std::move exist?. I am not asking about a mechanism to

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 14:40

    The function provided below is a bad idea. Don't use it. It provides a very easy path towards dangling references. I'd consider the code that needs it faulty and act accordingly.

    Still, I think this is an interesting exercise for some reason, so I can't help but show it.

    Inside a function, the names of its arguments are lvalues, even if the arguments are rvalue references. You can use that property to return an argument as an lvalue reference.

    template 
    constexpr T& as_lvalue(T&& t) {
        return t;
    };
    

提交回复
热议问题