Can overload functions by default / delete ref refifiers functions explicitly?

INTRO

  • Qualifying qualifiers : a way to disband the rl-valuness of an implied object. Take the following class as a quick example.

    class example { int member; public: // ... int& value() &; // ^ int&& value() &&; // ^^ int const& value() const&; // ^ }; 

    Using this C ++ 11 function (c ^ syntax) allows us to control the version of value() that will be called using

    • l-value
    • temporaries
    • const l-values

    In practice, the ref qualification applies to the *this class

  • Default / Remote Functions : specify a special member function as having a generated compiler ( default ) or not available ( delete ). Take as an example

     struct type { type(const type&) = delete; type& operator=(const type&) = delete; }; 

    The above structure allows you not to copy with extremely clear semantics

Questions

  • Is it possible / true to combine these functions ?
  • What are some cases where explicitly forbidden or bad style ?
  • Is there any use case / pattern for such a combination? (For example, creating conditional interfaces based on rl-valueness is quick and easy)
+6
source share
1 answer

Yes, but there is little use, since constructors and destructors cannot be ref-qual.

You can ref-qualify assignment statements:

 struct S { S &operator=(const S &) && = default; }; int main() { S s; S() = s; // OK s = S(); // error - LHS must be an rvalue } 

However, I am a little at a loss to imagine what this would be useful for.

+4
source

Source: https://habr.com/ru/post/970314/


All Articles