How I use references

栏目: IT技术 · 发布时间: 5年前

内容简介:Following a blog post byA bit of code is better than a blob of text:I try to never use pointers, at least in interfaces. Pointers should be non-owning, but in practice, there is too much C and legacy code for this to be the case. When I see a pointer, I am

Following a blog post by Herb Sutter , let me tell you how and when I use references.

  • If I do not need to mutate an input parameter, I will use a const reference, unless I know that copying is cheaper (When in doubt use a reference).
  • If I do need a copy of the parameter, I accept it by value and move it
  • If I do need to mutate an input parameter, I will accept an input reference. But in many cases, I prefer to take the parameter by value and return a copy.
  • I avoid out parameters. Returning by value is cheap, always preferable.
  • I use references and const references to make local aliases.
  • I avoidrvalue references

A bit of code is better than a blob of text:

void f(const Foo &); // input parameter
void f(Foo);         // input parameter, modified or sinked
void f(Foo &);       // input-output
Foo f();             // output

I try to never use pointers, at least in interfaces. Pointers should be non-owning, but in practice, there is too much C and legacy code for this to be the case. When I see a pointer, I am wary of ownership and lifetime. I always will be, because we cannot magically get rid of owning pointers.

These are general advices, and I recommend to follow them in in interfaces. Be adventurous at your own risk else where and use good judgment!

What if I need an optional reference?

Use a default parameter if you can, a pointer if you must.

std::optional<T&> would be an obvious answer, sadly it is something the committee refuses to standardize mainly because we cannot agree on what assignment and comparison should do. The answer is however quite simple: these operations should not be provided . I don’t know why but many people seem compelled to provide operators for everything when it doesn’t make sense or is otherwise confusing. When there is no good default, do not try to provide a default

Here is how to support reference in optional without having to specialize it.

template<class T>
  class optional {
  public:
    [[deprecated]] template<class U = T>
    requires std::semiregular<T>
    optional& operator=(U&&);
  };

template<class T, class U>
requires (std::regular<T> && std::regular<U>)
constexpr bool operator==(const optional<T>&, const optional<U>&);

This would be nothing new - we do that for views. Wrapper objects should never try to expose more regularity than the wrapped type.

We removed span::operator== and, unsurprisingly, exactly nobody is missing it. Instead of endless, unsolvable debates of what a given operation should do, a better question is: is that operation useful? The answer here is no, look at usages.

And this is in line with Herb Sutter’s argument that references are mostly useful as return values and parameters.

What about not_null, object_ptr, observer_ptr etc?

optional<Foo&> opt{foo}; is valid by construction. As long as you never use pointers, the only way to misuse optional is to deference it while it is not engaged.

Types constructed from a pointer give you twice as many opportunities to blow your foot off: while referencing and while constructing. It’s shifting a compile-time error to a runtime one…not the right direction to move in! Additionally, optional provides useful functions like value_or , or_else , transform

Are reference just pointers?

It’s memory addresses all the way down but it doesn’t matter how references are implemented. Because they are non-null and can’t be rebound, they behave as aliases, tied to the lifetime of the aliased object.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

REST实战

REST实战

Jim Webber、Savas Parastatidis、Ian Robinson / 李锟、俞黎敏、马钧、崔毅 / 东南大学出版社 / 2011-10 / 78.00元

为何典型的企业项目无法像你为web所开发的项目那样运行得如此平滑?对于建造分布式和企业级的应用来说,rest架构风格真的提供了一个可行的替代选择吗? 在这本富有洞察力的书中,三位soa专家对于rest进行了讲求实际的解释,并且通过将web的指导原理应用到普通的企业计算问题中,向你展示了如何开发简单的、优雅的分布式超媒体系统。你将会学习到很多技术,并且随着一家典型的公司从最初的小企业逐渐成长为......一起来看看 《REST实战》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换