Move-assignment and reference member

后端 未结 2 471
太阳男子
太阳男子 2020-12-09 17:14

Copy-assignment for a class with a reference member variable is a no-no because you can\'t reassign the reference. But what about move-assignment? I tried simply mov

相关标签:
2条回答
  • 2020-12-09 17:45

    (Posted as an answer from comment as suggested by the OP)

    In general if one wants to do non-trivial stuff with references in C++, one would be using reference_wrapper<T>, which is essentially a fancy value-semantic stand-in for a T&, then one would be done with it - it already provides (re)assignment and other operations. I'm sure that would make move constructor and assignment near-trivial, if not trivial (note not trivial as in per the is_trivially_* semantics).

    "Reference wrapper" is added to C++03 as part of TR1, and is part of C++11.

    Documentation: http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper

    0 讨论(0)
  • 2020-12-09 17:53

    A reference is, in some sense, a T *const with syntactic sugar on top to auto-derefence, auto-capture, and auto-lifetime extend temporaries. (note that this is not quite true, but often is in practice and practicality)

    If you want a reseatable reference, C++ has those: they are called pointers. You can use an accessor to replace dereferencing with a function call if you like. The remaining feature (temporary lifetime extension) that is hard to emulate does not apply to struct members.

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