什么是C ++临时工? [英] What are C++ temporaries?

查看:148
本文介绍了什么是C ++临时工?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Eckel中的常量章节,并且在解释临时性部分中感到困惑。我能得到的是,当我们将引用传递给函数时,编译器会创建一个临时对象(即const对象),因此即使将引用传递为

I was reading the constant chapter in Eckel and got mixed up in the part where Temporaries were explained . What I could get was that when we pass the reference to a function , the compiler creates a temporary which is a const object and so we can't modify it even if we pass a reference as

f(int &a){}

现在,我试图在网上查看一些其他有关临时人员的参考资料,并陷入困境


http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index .jsp?topic =%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr382.htm


在C ++中是否所有临时变量都为右值?


提示我,临时变量不仅仅是传递引用和在函数内部为此创建const对象。现在我可以从这两个链接中得到一些东西,但是我并不是说我已经了解了临时工作的整体功能,功能和用途。如果有人可以解释临时工的概念,那将真的很有帮助。提前谢谢。
布鲁斯·埃克尔(Bruce eckel)的原始示例是:

Now I tried to look at some other references for Temporaries online and got stuck up

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr382.htm and

Are all temporaries rvalues in C++?

which prompted me that temporaries are much more than just passing references and creating const object for that inside the functions.Now I could get something out of these two links but could not say that I have understood the working , functionality and use of temporaries as a whole. It would be really helpful if someone could explain the concept of temporaries. Thanks in advance.
The original example of bruce eckel is:

// Result cannot be used as an lvalue
class X {
    int i;
    public:
    X(int ii = 0);
    void modify();
};
X::X(int ii) { i = ii; }
void X::modify() { i++; }
X f5() {
    return X();
}
const X f6() {
    return X();
}
void f7(X& x) { // Pass by non-const reference
    x.modify();
}
int main() {
    f5() = X(1); // OK -- non-const return value
    f5().modify(); // OK
    // Causes compile-time errors:
    //! f7(f5());
    //! f6() = X(1);
    //! f6().modify();
    //! f7(f6());
} ///:~


推荐答案

在C ++临时对象是编译器在各种上下文中创建的未命名对象。典型用法包括引用初始化,参数传递,表达式求值(包括标准类型转换),函数返回和异常(引发表达式)。

In C++ temporaries are unnamed objects that compiler creates in various contexts. The typical uses include reference initialization, argument passing, evaluation of expressions (including standard type conversions), function returns, and exceptions (throw expressions).

href = http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr382.htm rel = nofollow>链接:

As from this link:

创建临时对象以初始化引用变量时,该临时对象的名称与引用变量的作用域相同。当在评估全表达式(一个表达式,而不是另一个表达式的子表达式)期间创建一个临时对象时,该临时对象将被销毁,这是其评估的最后一步,即在词法上包含了创建对象的位置。

When a temporary object is created to initialize a reference variable, the name of the temporary object has the same scope as that of the reference variable. When a temporary object is created during the evaluation of a full-expression (an expression that is not a subexpression of another expression), it is destroyed as the last step in its evaluation that lexically contains the point where it was created.

销毁全表达式有一些例外:

There are exceptions in the destruction of full-expressions:


  1. 表达式显示为定义对象的声明的初始化程序:初始化完成后,临时对象将被销毁。

  2. 引用绑定到临时对象:临时对象在引用的末尾销毁

如果使用构造函数为类创建了一个临时对象,则编译器将调用适当的(匹配)构造函数来创建临时对象

If a temporary object is created for a class with constructors, the compiler calls the appropriate (matching) constructor to create the temporary object.

当临时对象被销毁并且析构函数存在时,编译器将调用析构函数销毁该临时对象。当您退出创建临时对象的范围时,该对象将被销毁。如果引用绑定到临时对象,则除非引用因控制流中断而被破坏,否则当引用超出范围时,该临时对象将被销毁。例如,构造函数初始化器为引用成员创建的临时对象在离开构造函数时将被销毁。

When a temporary object is destroyed and a destructor exists, the compiler calls the destructor to destroy the temporary object. When you exit from the scope in which the temporary object was created, it is destroyed. If a reference is bound to a temporary object, the temporary object is destroyed when the reference passes out of scope unless it is destroyed earlier by a break in the flow of control. For example, a temporary object created by a constructor initializer for a reference member is destroyed on leaving the constructor.

在这种临时对象是多余的情况下,编译器不会构造它们,以便创建更有效的优化代码。当您调试程序时,尤其是对于内存问题,可能会考虑此行为。

In cases where such temporary objects are redundant, the compiler does not construct them, in order to create more efficient optimized code. This behavior could be a consideration when you are debugging your programs, especially for memory problems.

让我们对其进行总结以这种方式。可以出于以下原因创建这些临时对象:

Let us summarize it this way. These temporary objects can be created for the following reasons:


  1. 使用与该类型不同的初始化器初始化const引用

  1. To initialize a const reference with an initializer of a type different from that of the underlying type of the reference being initialized.

存储要返回用户定义类型的函数的返回值。仅当您的程序未将返回值复制到对象时,才创建这些临时对象。因为没有将返回值复制到另一个对象,所以将创建一个临时对象。创建临时对象的更常见情况是在对表达式进行求值时,必须调用重载的运算符。这些重载的运算符函数返回一个用户定义的类型,该类型通常不会复制到另一个对象。

To store the return value of a function that returns a user-defined type. These temporaries are created only if your program does not copy the return value to an object. Because the return value is not copied to another object, a temporary object is created. A more common case where temporaries are created is during the evaluation of an expression where overloaded operator functions must be called. These overloaded operator functions return a user-defined type that often is not copied to another object.

将转换结果存储为用户定义的类型。当将给定类型的对象显式转换为用户定义的类型时,该新对象将被构造为临时对象。

To store the result of a cast to a user-defined type. When an object of a given type is explicitly converted to a user-defined type, that new object is constructed as a temporary object.

让我们考虑示例

class X {
   / / ...
public:
   / / ...
   X(int);
   X(const X&);
   ~X();
};

X f(X);

void g()
{
   X a(1);
   X b = f(X(2));
   a = f(a);
}

在这里,实现可能会使用临时结构来构造 X(2),然后使用 X 将其传递给 f()复制构造函数;或者,可以在用于保存参数的空间中构造 X(2)。另外,在使用 X f(X(2()))的结果。 c>的复制构造函数;或者, f()的结果可以在 b 中构造。另一方面,表达式 a = f(a)需要一个临时结果 f(a) ,然后将其分配给 a

Here, an implementation might use a temporary in which to construct X(2) before passing it to f() using X’s copy-constructor; alternatively, X(2) might be constructed in the space used to hold the argument. Also, a temporary might be used to hold the result of f(X(2)) before copying it to b using X’s copy-constructor; alternatively, f()’s result might be constructed in b. On the other hand, the expression a=f(a) requires a temporary for the result of f(a), which is then assigned to a.

这篇关于什么是C ++临时工?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆