Herramientas de usuario

Herramientas del sitio


wiki2:cpp:idiomatic

¡Esta es una revisión vieja del documento!


Idiomatic C++

Techniques

Value categories

Main C++ expression types are rvalue and lvalue. It depends on which part of an assignment they appear (right-hand: rvalue, left-hand: lvalue).

Mainly, an lvalue refers to an object that persists beyond a single expression (all variables, including nonmodifiable (const) variables, are lvalues). An rvalue is a temporary value that does not persist beyond the expression that uses it.

In the next example, x is an lvalue because it persists beyond the expression that defines it. The expression 3 + 4 is an rvalue because it evaluates to a temporary value that does not persist beyond the expression that defines it.

#include <iostream>
using namespace std;
int main()
{
   int x = 3 + 4;
   cout << x << endl;
}
 // Incorrect usage: The left operand must be an lvalue (C2106).
7 = i; // C2106
j * 4 = 7; // C2106
 
// Correct usage: the dereferenced pointer is an lvalue.
*p = i; 
 
const int ci = 7;
// Incorrect usage: the variable is a non-modifiable lvalue (C3892).
ci = 9; // C3892
 
// Correct usage: the conditional operator returns an lvalue.
((i < 3) ? i : j) = 7;
 

RAII

Precompiled headers

Forward declaration

Pimpl

Resources

Libraries

Notes

Speed up compiler times

wiki2/cpp/idiomatic.1446382499.txt.gz · Última modificación: 2020/05/09 09:25 (editor externo)