问题标签 [const-reference]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
911 浏览

c++ - 从函数返回临时对象的引用

考虑以下代码 -

输出是 -

为什么在打印变量的地址后值会改变k?如果我用 替换该行const int& k = retRef()const int& k = 6;则输出符合预期。

为什么会有这种不同的行为?提前致谢

0 投票
1 回答
592 浏览

c++ - 左值和右值作为函数参数

我试图理解 C++ 中的左值和右值。

所以我将它们用作传递给函数的参数。在第一种情况下,我有两个函数,第一个引用了一个 const int,在这种情况下,感谢“const”(参见链接),我可以将 Lvalue 和 Rvalue 都传递给第一个函数,我不会有任何问题. 相反,在第二个函数中,我必须传递一个 Rvlaue,否则我会得到描述的错误。

好的!

为什么如果第二个函数成为函数模板,如下例所示,我也可以传递一个左值。

0 投票
1 回答
494 浏览

c++ - 初始化列表中对象的生命周期可以延长吗?

我的印象std::initializer_list可能类似于 C++ 中的文字字符串,甚至更进一步,它们可能会延长 const 引用的生命周期。这是正确的评价吗?

以后可以在本地范围内initializer_list以某种方式引用(不复制它们)中的对象吗?在全球范围内?

例如,这个测试在 GCC 和 clang 中通过。这只是偶然吗?

0 投票
2 回答
3119 浏览

c++ - 为什么`const T&`不确定是const?

请注意:b是的const T&并且++b是好的!

为什么const T&不确定是const?

0 投票
4 回答
142 浏览

c++ - A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

In C++, can the value of a const & change?

Well, of course it cannot change, can it? That's what const means. Moreover, listen to Stroustrup:

A const lvalue reference refers to a constant, which is immutable from the point of view of the user of the reference.

But what about this?

On my machine, this outputs, old_r == 0 but new_r == 1.

That gets to my real question. In the above code, look at the line

Insofar as

  • the address &new_r is extracted neither on this line nor elsewhere in the code and
  • the code has nothing volatile,

does anything prevent an optimizing compiler from merging old_r and new_r into a single constant object, treating the line as though it read as follows?

I ask because, as far as I know, if the compiler did so optimize, that might alter the behavior. The program might output, old_r == 0 but new_r == 0.

RELATED QUESTIONS

The most nearly related existing question I find is this one:

The following are also related but, unlike the present question, involve casts:

See also N4659 (draft C++17 standard), sect. 10.1.7.1, "The cv-qualifiers."

The quote of Stroustrup at the top of the question comes from sect. 7.7.2 of The C++ Programming Language, 4th ed. Of course, no author can write every sentence perfectly in a thousand-page book; yet perhaps Stroustrup is clear and I have merely read him wrong. Nevertheless, you might see why the sentence has confused me. This is why I have asked.

0 投票
1 回答
109 浏览

c++ - const ref 绑定到右值的内部资源

考虑代码:

const Resource& r = ResourceContainer("foo").myResource;

标准对 myResource 的生命周期有何规定?

类似,但不确定这是否等效:现在想象它是通过operator const Resource&(). 这有什么区别吗?

const Resource& r = ResourceContainer("foo");

0 投票
0 回答
24 浏览

c++ - 为什么 PVS-Studio 声明存在非常量参考?

我必须首先承认我对 C++ 的经验很少,所以如果这个问题看起来有些愚蠢,请原谅我。

在使用 PVS-Studio 分析项目时,我发现了一个令人费解的元素。这是重现问题的代码:

当然,这里不需要这个赋值运算符,但问题就在 PVS-Studio 生成的警告中:

V790 赋值运算符通过非常量引用获取对象并返回该对象,这很奇怪。

(强调我的)

我不知道该怎么想。这个非常量引用在哪里?赋值运算符的参数是const Test &,即它似乎是常量引用。此外,return test仅当分配无论如何都是无操作时才执行,即返回*this并且test应该没有区别。

这是误报,还是我根本不明白什么?

0 投票
1 回答
49 浏览

c++ - 将 const 引用/指针传递给用于存储的类的首选方法,而不是复制引用的对象

例子:

所以目标是存储指向某个外部对象的 const 指针,而不是存储外部对象内容的副本。

  • 通过 const 引用传递:Foo(const Bar& bar)
    • 将对象传递给构造函数的常见习语,许多程序员会认为 Foo 将存储 Bar 的副本而不是指针/引用本身。
    • 调用者可以通过一个临时的。这可以通过删除右值重载来防止Foo(const Bar&&) = delete。然而,这并不是万无一失的,因为您仍然可以通过一个接受 const 引用并返回 const 引用的函数来偷运临时对象。Foo afoo(std::min(Bar{}, anotherBar));例如。
  • 通过 const 指针传递:Foo(const Bar* bar)
    • 必须检查 nullptr,这是一个运行时检查,编译时机制是首选。
    • 仍可临时走私:Foo afoo(&std::min(Bar{}, anotherBar));

以上哪个是首选,为什么?或者有没有更好的方法来做到这一点,不会受到上述问题的影响?

0 投票
1 回答
54 浏览

c++ - 为什么我的 operator=(T&&) 模板只绑定到 const& 而不是 &&?

从右值引用和 const 引用编写重载函数时,您可能会出现代码重复,因此我有时会使用相同的代码。如此处所示:

现在我的理解是这应该同时实现 anA& operator=(T const&)和 an A& operator=(T&&)。所以给定这段代码,理论上我会期望这个调用:

产生以下输出:

然而,令我惊讶的是,第二行(!)不见了。我怎样才能同时覆盖两者?


我正在使用 g++ 8.3.0 和-std=c++17.

0 投票
3 回答
119 浏览

c++ - 为什么 GCC 在复制赋值操作中拒绝 const 引用?

我想正常重载一个常见的复制赋值运算符。一开始我使用了一个只需要对源进行 const 引用的接口,并显式禁用了接受可修改引用的接口,但我无法通过编译。编译器报告“错误:使用已删除的函数 'ClassA& ClassA::operator=(ClassA&)”

当然,如果我不明确删除接口,我可以编译,但这不是我的目的。我想明确删除它,以避免意外使用它。

为什么复制赋值操作需要对源的可修改引用,而不是 const 引用?赋值操作只需要以只读方式访问源!

关于复制构造函数也有同样的问题,为了简化我省略了它。

我的代码有什么问题?或者我们不能删除它?

我的示例代码如下: