C++: God-like Left Value
About the left value:

First of all, you should know that computers are a branch of mathematics, so an idiom developed as a formal language at first is the grammatical distinction between left values and right values, which is also inherited by most languages today.

In this regard, please Baidu search an article on csdn: the essence of C/C++ left value.

Of course, the distinction between left and right values in some languages has gradually faded, or you can think that it has become more complicated. The following is the explanation of the left and right values in c++ 1 1:

The definitions of LValue and RValue in C++ 1 1 are more detailed and reasonable (but also more troublesome).

Chapter 1: Introduction to the New Classification of C++ 1 1

Chapter 2: Conversion between Different Values

Chapter 3: How to distinguish the values of some common expressions?

1. Expression classification

The so-called value is for the expression. An expression can be divided into the following values, which are explained in detail below.

1. 1. Left value (left value)

A left value refers to a function or object. For example:

E is a pointer, so *E is a left value.

The return value of the function is a left reference, and the return value is lvalue. Such as int&foo ();

1.2.xvalue (expiration value)

Xvalue refers to an object, but unlike lvalue, this object is about to die. Specifically, xvalue is an expression that contains a right value reference. Because right-valued reference is a new thing introduced by C++ 1 1, xvalue is also a new thing. For example:

The return value of the function is a right-value reference, and its return value is xvalue. For example, int&&foo ();

1.3.glvalue (generalized left value, universal left value)

Glvalue is the collective name of lvalue and xvalue.

1.4. Right value (right value)

Rvale is a generic term for xvalue and prvalue. Because of the introduction of right value reference, the definition of right value is extended in C++.

1.5.prvalue (pure right value, pure right value)

Prvalue refers to a temporary object, a child of a temporary object, or a value that is not assigned to any object. Prvalue is the right value in the old standard. For example:

The return value of the function is normal, and the return value is right. Such as int foo ();

There is no value assigned to any object. Such as 5.3, really.

2. Conversion of expression value types

GL value →pr value

In fact, there is only one way to change expressions, that is,

Left value →pr value

X value →pr value

2. 1. Left value →pr value

This is very common: glvalue(lvalue and xvalue) can be implicitly converted into prvalue to meet the demand. For example:

The int & ampfoo(int val) // function accepts prvalue.

{ int a = val+ 1; Return to a; }

int main(){

int I = 5;

cout & lt& ltfoo(4)& lt; & ltendl// passed in a prvalue.

cout & lt& ltfoo(I)& lt; & ltendl// passed in a left value.

cout & lt& ltfoo(foo(I))& lt; & ltendl// foo(i) returns lvalue and passes it to the external function.

System ("suspended");

}

Of course, this function is not well written, so don't pass temporary objects out of the function ~

If foo is rewritten as follows, the above code will not work, because prvalue→lvalue is not drop ~

1234 int & amp; foo(int val){ return val+ 1; }

2.2. Decomposition of CV-Qualifier

In other transformations, const transformation cannot be deleted, but the transformation from glvalue to prvalue can be deleted. This rhetoric is obscure, but it is actually very common, such as:

const int a = 5; int b = a; // b =? This requires a right value, so a is converted to the right value first, and the const qualifier is discarded.

3. Determination of expression value type

Every expression in C++ has its value type. In other words, any expression belongs to one of the following three categories: lvalue, xvalue and prvalue.

If it is a simple expression, it is naturally easy to understand, such as variables, constants and return values listed in the previous section. But most cases are much more complicated.

2. 1. C++ judgment of built-in operation

This is about specific operation and analysis. The built-in operation of C++ defines in detail the types of operands and the types represented by the whole operation. For example:

Allocation operation op 1 = op2. Op 1 is the left value, and op2 is the right value. The whole operation is lvalue.

2.2. User-defined operations

A custom operation is nothing more than a self-built function. (Well, yes, operator overloading in C++ is also function overloading ~ ~) Operands are equivalent to function parameters, and operation yield values are equivalent to function return values. Its type is defined by the user. For example:

int foo(int); Parameter is prvalue, and the return value is prvalue.

int & ampfoo(int); The parameter is prvalue and the return value is lvalue. u

On the second question:

First of all, I need to warn you that if you really encounter this kind of problem in reality, your design and implementation must be problematic, and it may be serious. Ignore this for the time being ... let's talk about forced type conversion first.

Before you are familiar with the type conversion of C++, you should first know that there are two types conversion modes in C: mandatory conversion and implicit conversion.

C++ decided to avoid implicit type conversion as much as possible at the beginning of design. As can be seen from the later standard update, the static_cast keyword was proposed to replace implicit type conversion. Static_cast was originally used to replace implicit type conversion to eliminate any unsafe implicit conversion, so static_cast allows some conversions warned by compiler to pass directly, so it is a kind of conversion that users can guarantee safety.

Reinterpret _ cast & lt& gt is simply a forced type conversion in C!

So func () has nothing to do with goods whose surplus value is not surplus value.

The solution is as follows:

func(static _ cast & lt; wchar _ t * & gt(static _ cast & ltvoid * & gt(& ampc)));

Or func((wchar _ t *)(&;) (&; c))