什么是ref-qualifier的使用`const&&`? [英] What is use of the ref-qualifier `const &&`?

查看:281
本文介绍了什么是ref-qualifier的使用`const&&`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面的代码示例:

  #include< iostream> 
#include< string>
#include< utility>

struct A {
std :: string abc =abc;
std :: string& get()& {
std :: cout<< get()& << std :: endl;
return abc;
}
std :: string get()&& {
std :: cout<< get()&& << std :: endl;
return std :: move(abc);
}
std :: string const& get()const& {
std :: cout<< get()const& << std :: endl;
return abc;
}
std :: string get()const&& {
std :: cout<< get()const&& << std :: endl;
return abc;
}
};

int main()
{
A a1;
a1.get();
const A a2 {};
a2.get();
A()。get();
const A a3 {};
std :: move(a3).get();
}

输出结果如下所示:


get()&

get()const&

get()&&&

get()const&&&


这会编译并运行clang和gcc 4.9.1(不是4.9.0)。直播这里取样



在一般代码中(样例是看代码如何编译和运行)。




  • 方法的 const&& ref-qualifier中的



该方法无法修改对象上的内容( const ),尝试 return std :: move(abc); 方法实际上并不移动 std :: string 在所有。大概你会希望能够修改对象,因为它是一个r值,并且不会很长时间。如果要删除 const&& 限定方法,代码 std :: move(a3).method()会绑定到 const& 限定方法,这是有意义的。




  • 如果有的话,隐含的语义差异在一个限定为 const& 的方法和一个限定为 const&& ?也就是说 cb> cd> 从临时对象中删除?

  • std :: string get()const&&< / code> c> const&&& ...(一般)



    ;& 成员方法上的限定符最少是最小的。该对象不能以与&& 方法相同的方式修改;它是 const 毕竟(注意, mutable 确实改变了这一点)。因此,我们将无法揭开它的胆量,因为暂时性正在过期,因为我们在类似于正常移动的东西。



    在许多方面, const&& 的有用性可以在类型 const T&&& 是从开始。 const T&&& 函数参数有多有用?正如在此此处的另一个答案中指出的,它们在声明函数删除时非常有用,例如在这种情况下

     模板< class T& void ref(const T&&)= delete; 

    明确禁止prvalue和xvalue值类别类型的对象与函数一起使用, const T&&&&< / code> does 绑定到所有prvalue和xvalue对象


    const&&& 方法限定符?


    a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4082.pdf =nofollowtitle =C ++扩展库库基础> C ++库扩展,可选,第5.3节包括重载,例如

      constexpr T value()const&& ;;符合 const&& 



    <并指定执行与&& 替代方法相同的操作。



    这个案例;这是为了完整性和正确性。如果对$ rvalue调用 value()方法,则它执行相同的动作,与 const 不。 const 将需要由正在移动的包含对象或使用它的客户端代码处理。如果有一些包含对象的 mutable 状态,那么这个状态可以合法地改变。



    还是有一些优点在这里; 禁止方法在prvalues和xvalues中使用。


  • 如果类型具有可变状态,且限定符有意义

  • 如果您要实现通用容器类型,则完整性和正确性,请考虑添加它并执行相同的操作动作作为&& 方法。




什么是canonical签名看起来像一个 const&& 合格方法?


方法将执行与&& 方法相同的操作,我主张签名匹配&& 签名。


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

Given the code sample below;

#include <iostream>
#include <string>
#include <utility>

struct A {
  std::string abc = "abc";
  std::string& get() & {
    std::cout << "get() &" << std::endl;
    return abc;
  }
  std::string get() && {
    std::cout << "get() &&" << std::endl;
    return std::move(abc);
  }
  std::string const& get() const & {
    std::cout << "get() const &" << std::endl;
    return abc;
  }
  std::string get() const && {
    std::cout << "get() const &&" << std::endl;
    return abc;
  }
};

int main()
{
  A a1;
  a1.get();
  const A a2{};
  a2.get();
  A().get();
  const A a3{};
  std::move(a3).get();
}

And the output is as you would expect:

get() &
get() const &
get() &&
get() const &&

This compiles and runs with clang and gcc 4.9.1 (not 4.9.0 though). Live sample here.

In general code (the sample is there to see how the code compiles and runs).

  • What would the purpose of the const && ref-qualifier on a method be?

The method is unable to modify the contents on the object (it is const), an attempt to return std::move(abc); from the const && method doesn't actually move the std::string at all. Presumably you would want to be able modify the object, since it's an r-value and won't be around for long. If the const && qualified method were to be removed, the code std::move(a3).method() would bind to the const & qualified method, which would make sense.

  • What, if any, would the implied semantic difference be between a method qualified as const & and one qualified as const &&? I.e. how would the implementation vary or why would you want both?
  • Would the std::string truely be able to be "moved" out of the temporary object?
  • What would a "canonical" signature look like for std::string get() const && in this case?

解决方案

On the usefulness of const&&... (in general)

The usefulness of the const&& qualifier on the member method is minimal at best. The object cannot be modified in the same manner as a && method would allow it to be modified; it is const after all (as noted, mutable does change this). So we will not be able to rip out its guts, since the temporary is expiring anyway, as we would in something akin to a normal move.

In many ways the usefulness of the const&& may be best evaluated in the context of how useful an object of type const T&& is to begin with. How useful is a const T&& function argument? As pointed out in another answer (to this question) here, they are very useful in declaring functions deleted, e.g. in this case

template <class T> void ref (const T&&) = delete;

to explicitly disallow objects of prvalue and xvalue value category types from being used with the functions, and const T&& does bind to all prvalue and xvalue objects.

What is the usefulness of const&& method qualifier?

It is interesting to note that in the proposal C++ library extensions, optional, § 5.3, includes overloads, e.g.

constexpr T value() const &&;

that are qualified as const&& and are specified to perform the same action as the && alternative.

The reason I can infer for this case; is that this is for completeness and correctness. If the value() method is called on an rvalue, then it performs the same action independent of it being const or not. The const will need to be dealt with by the contained object being moved or the client code using it. If there is some mutable state with the object being contained, then that state can legitimately be changed.

There may well still be some merit in this; in no particular order...

  • To declare it = delete to prohibit the method's use on prvalues and xvalues.
  • If the type has mutable state and the qualifier makes sense (possibly in addition to the other qualifiers) in the target environment, consider it.
  • If you are implementing a generic container type, then for completeness and correctness, consider adding it and performing the same action as the && method. Advice here is sort from the standard library (and its extensions).

What would a "canonical" signature look like for a const&& qualified method?

Since the method will be performing the same action as the && method, I would advocate that the signature matches the && signature.

这篇关于什么是ref-qualifier的使用`const&amp;&amp;`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆