What is use of the ref-qualifier `const &&`?

后端 未结 4 602
予麋鹿
予麋鹿 2020-12-13 19:06

I\'ve been digging around ref-qualifiers a bit, following on a previous question.

Given the code sample below;

#include 
#include <         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 19:37

    Suppose we have a type with a mutable state. Then const&& will both allow us to mutate that state, and indicate that such mutation is safe.

    struct bar;
    
    struct foo {
      mutable std::vector state;
      operator bar() const&;
      operator bar() const&&;
    };
    

    const is not absolute.

    Barring mutable state, it is not safe to cast away const in a const&& method in order to extract state, because extracting state in this way from an actual const object is undefined behavior.

提交回复
热议问题