6

INTRO

  • Ref qualifiers : A way to dissambiguate the rl-valuness of the implied object. As a quick example, take the following class

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

    The use of this C++11 feature (syntax marked with ^), allows us to control the version of value() that will be called with

    • l-values
    • temporaries
    • const l-values

    Practically the ref qualification applies to the classe's *this

  • Defaulted / Deleted functions : Specify a special member function as having the compiler generated (default) definition or inaccessible (delete). As an example take

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

    The above struct, achieves being non copyable with extremely clear semantics

QUESTIONs

  • Is it possible / valid to combine these features ?
  • Which are the cases where it's explicitly forbidden or bad style ?
  • Is there any use case / pattern for such a combination ? (Eg creating conditional interfaces based rl-valueness quick and easy)
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • Somewhat related: http://stackoverflow.com/questions/20886466/should-the-assignment-operator-observe-the-assigned-objects-rvalueness – dyp Jun 04 '14 at 19:31

1 Answers1

4

Yes, but there's not much use, as constructors and destructors can't be ref-qualified.

You can ref-qualify assignment operators:

struct S {
  S &operator=(const S &) && = default;
};

int main() {
  S s;
  S() = s;  // OK
  s = S();  // error - LHS must be an rvalue
}

However, I'm somewhat at a loss to imagine what this would be useful for.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • 4
    Since you can `= delete` just about anything, this is very useful for clear error messages (instead of "overload not found"). – Xeo Jun 04 '14 at 13:27