Workarounds for no 'rvalue references to *this' feature

前端 未结 1 1228
暖寄归人
暖寄归人 2021-01-30 12:37

I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy i

相关标签:
1条回答
  • 2021-01-30 13:02

    Good question. I attempted writing a similar sort of proxy class recently but never achieved a good solution. The best I found was calling a member function on every use where the proxy was required to be an r-value:

    ORef<T> move() {
        return ORef<T>( this->release() );
    }
    

    This changes the semantics of declaring something an r-value from std::move(proxy) to proxy.move(), but also allows the possibility of returning an object of a different type (implicitly convertible to your required type).

    My coding practice using this was to always pass proxy objects as rvalues which forced manual specification of semantics (move, shared reference, copy or whatever), but that of course makes usage errors a potential problem (e.g. calling x.move() prior to the final usage of x).

    0 讨论(0)
提交回复
热议问题