How I use references

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

内容简介: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.


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

查看所有标签

猜你喜欢:

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

程序员面试手册

程序员面试手册

[印] 纳拉辛哈·卡鲁曼希(Narasimha Karumanchi) / 爱飞翔 / 机械工业出版社 / 2018-2-27 / 99

本书特色 以通俗易懂的方式讲述面试题,涵盖编程基础、架构设计、网络技术、数据库技术、数据结构及算法等主题 书中的题目来自微软、谷歌、亚马逊、雅虎、Oracle、Facebook等大公司的面试题,以及一些知名竞赛(如GATE)的考试题 全书约有700道算法题,每道题都有详细解答 针对每一编程问题,都会按照复杂度递减的顺序给出各种解法 专注于问题本身并对这些问题做出分析,......一起来看看 《程序员面试手册》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具