无法从可转换类型初始化非常量引用

发布于 2024-08-26 22:35:08 字数 442 浏览 5 评论 0原文

我无法从可转换类型 T2 初始化对类型 T1 的非常量引用。但是,我可以使用 const 引用。

long l;
const long long &const_ref = l; // fine
long long &ref = l;             // error: invalid initialization of reference of
                                // type 'long long int&' from expression of type
                                // 'long int'

我遇到的大多数问题都与无法分配给非常量引用的右值有关。这里的情况并非如此——有人能解释一下吗?谢谢。

I cannot initialize a non-const reference to type T1 from a convertible type T2. However, I can with a const reference.

long l;
const long long &const_ref = l; // fine
long long &ref = l;             // error: invalid initialization of reference of
                                // type 'long long int&' from expression of type
                                // 'long int'

Most problems I encountered were related to r-values that cannot be assigned to a non-const reference. This is not the case here -- can someone explain? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征﹌骨岁月お 2024-09-02 22:35:08

整数提升会产生右值。 long 可以提升为 long long,然后绑定到 const 引用。就像您已经完成的那样:

typedef long long type;
const type& x = type(l); // temporary!

相反,如您所知,右值不能绑定到非常量引用。 (毕竟没有实际的long long可以引用。)

An integer promotion results in an rvalue. long can be promoted to a long long, and then it gets bound to a const reference. Just as if you had done:

typedef long long type;
const type& x = type(l); // temporary!

Contrarily an rvalue, as you know, cannot be bound to a non-const reference. (After all, there is no actual long long to refer to.)

千里故人稀 2024-09-02 22:35:08

long long 的大小不一定等于 long,甚至可能使用完全不同的内部表示形式。因此,您不能将对 long 的非常量引用绑定到 long long 类型的对象,反之亦然。标准禁止它,并且您的编译器不允许它是正确的。

您可能会对以下代码片段有同样的疑问:

long a = 0; 
long long b = 0;

a = b; // works!

long *pa = 0;
long long *pb = pa;

最后的初始化将不起作用。仅仅因为一种类型可以转换为另一种类型,并不意味着复合其中一种类型的另一种类型可以转换为复合另一种类型的第三种类型。同样,对于以下情况

struct A { long i; };
struct B { long long i; };

A a;
B b = a; // fail!

在这种情况下 AB 分别复合类型 longlong long,很像 long&long long& 复合这些类型。然而,仅仅因为这个事实,它们就不能相互转换。其他规则也适用。

如果引用是 const,则会创建一个具有正确类型的临时对象,然后将该引用绑定到该对象。

long long is not necessarily sized equal to long and may even use an entire different internal representation. Therefor you cannot bind a non-const reference to long to an object of type long long or the other way around. The Standard forbids it, and your compiler is correct to not allow it.

You can wonder the same way about the following code snippet:

long a = 0; 
long long b = 0;

a = b; // works!

long *pa = 0;
long long *pb = pa;

The last initialization won't work. Just because a type is convertible to another one, doesn't mean another type that compounds one of them is convertible to a third type that compounds the other one. Likewise, for the following case

struct A { long i; };
struct B { long long i; };

A a;
B b = a; // fail!

In this case A and B each compound the type long and long long respectively, much like long& and long long& compound these types. However they won't be convertible into each other just because of that fact. Other rules happen to apply.

If the reference is to const, a temporary object is created that has the correct type, and the reference is then bound to that object.

淡淡離愁欲言轉身 2024-09-02 22:35:08

我不是标准律师,但我认为这是因为 long longlong 更宽。允许使用 const 引用,因为您不会更改 l 的值。常规引用可能会导致对于 l 来说太大的赋值,因此编译器不会允许它。

I'm not a standards lawyer, but I think this is because long long is wider than long. A const reference is permitted because you won't be changing the value of l. A regular reference might lead to an assignment that's too big for l, so the compiler won't allow it.

半岛未凉 2024-09-02 22:35:08

让我们假设这是可能的:

 long long &ref = l; 

这意味着稍后在代码中您可以将 ref 引用的值更改为大于 long 类型可以保存但可以用于 long long 的值。实际上,这意味着您覆盖了额外的内存字节,这些内存字节可以由不同的变量使用,从而产生不可预测的结果。

Let's assume that it's possible :

 long long &ref = l; 

It means that later in the code you can change the value referenced by ref to the value that is bigger then long type can hold but ok for long long. Practically, it means that you overwrite extra bytes of memory which can be used by different variable with unpredictable results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文