本文介绍了复制/分配基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准对基本类型的复制/分配有何看法?

What does the standard say about copy/assignment of fundamental types?

对于类类型,我们有复制构造函数,赋值运算符,该赋值运算符将右侧作为引用(必须是引用,否则我们将无限递归):

For class types, we have copy constructor, assignment operator, which takes the right hand side as a reference (it must be a reference, otherwise we had infinite recursion):

struct Foo {
    Foo(const Foo &);
};

这是如何定义基本类型的?

How does this defined for fundamental types?

看这个例子:

const Foo foo;
Foo f = foo;

const int a = 2;
int b = a;

在这里,f = foo; odr使用foo,因为复制构造函数需要引用,对吗?如果基本类型的副本具有参考参数,则b = a也将使用a.是这样吗如果没有,该如何处理?

Here, f = foo; odr-uses foo, as copy-constructor takes a reference, right?. If copy of fundamental types had a reference parameter, then b = a would odr-use a as well. Is it the case? If not, how is it handled?

推荐答案

我们可以跟踪它.从[dcl.init]开始.

We can trace it. Starting at [dcl.init].

在这种情况下,标准转换将是a上从左值到右值的转换.但这并不能使用a.我们在[basic.def.odr]

The standard conversion in this case would be the lvalue-to-rvalue conversion on a. But that doesn't odr-use a. For we see in [basic.def.odr]

a是一个常数表达式,用a替换上面的xex证明它拥有条件的另一半,因此不被滥用.

a is a constant expression and substitution of a for x and ex above demonstrates it holds the other half of the condition, so it's not odr-used.

这篇关于复制/分配基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 12:40