A detailed example of the spiral rule for C++ variable determination

  • 2020-06-19 11:16:33
  • OfStack

preface

One identifier in C++ is coupled with a variety of modifiers, making the intention of the identifier not obvious at first glance, and even requires careful analysis to know the specific meaning of the identifier.

Such as:


void (*signal(int, void (*fp)(int)))(int);

What is signal?

Corkscrew rule

An unofficial "clockwise/spiral rule" (Clockwise/Spiral Rule) is used to help identify variables.

The content of the law, in simple terms, in order to understand the meaning of an unknown identifier, we can:

1. Start from the identifier we need to determine, circle clockwise and replace with corresponding semantics when the following symbols are encountered:

[x] or [] = > Arrays or arrays of capacity x (type1,type2...) = > Receive type1, type2... Is a function whose return value is (to be determined). * = > A pointer to (type to be determined)

2. Repeat the above steps until all the symbols in the statement have been traversed.

3. Always give priority to parsing the parts enclosed in parentheses.

The ife

1 simple example

To start with a simple statement, determine the meaning of str in the following statement:

[

+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+

]

According to the spiral law, as shown in the chart above,

Start with str, the object that needs to be determined. The first encounter on the spiral path is [left square bracket, so we know that str is an array of size 10. Continue rotating and encounter *, so str is an array of size 10, with the array elements as Pointers. Go on, encounter; Identifies the end of the statement. Further on, you encounter char, so str is an array of size 10 with elements that point to a pointer of type char.

The advanced

Go back to the statement at the beginning of the article to determine the meaning of signal.

[

+-----------------------------+
| +---+ |
| +---+ |+-+| |
| ^ | |^ || |
void (*signal(int, void (*fp)(int)))(int);
^ ^ | ^ ^ || |
| +------+ | +--+| |
| +--------+ |
+----------------------------------+

]

The spiral rule is used to draw the diagram above, which can then be analyzed:

First encounter from the signal to be determined (left parenthesis, indicating that signal is a function, with int and...

Here, we need to take a step to determine the meaning of fp by using the spiral rule, and then confirm the complete participation of the function signal. Therefore, a subscrew was performed from fp.

Since the part enclosed in parentheses needs to be resolved first, the first thing you encounter when you turn 1 is *, so fp is 1 pointer.

To continue parsing fp, encounter (, so fp is a pointer to a function that receives one input of type int.

Go ahead and encounter void, so fp is a pointer to a function that receives an input of type int and returns a null value.

After the analysis of fp is completed, it can be known that the type of signal is:

Is a function with input parameter of: 1 int type 1 pointer to a function that receives 1 input of type int and returns a null value

The path runs into the spiral of signal and meets * (to the left of signal), so signal is

1 function, the input parameter is: 1 int type 1 pointer to a function that receives 1 input of type int and returns a null value The return value is a pointer

Further on, you encounter (, followed by above, and the return value is a pointer to another function that receives 1 int input.

Finally, void is encountered, and the return value of signal points to the null return value of this function.

Finally, the complete type of signal under 1 is: receive 1 int, 1 pointer to a function that receives 1 int and returns a null value, the function of these two parameters, and the return value is a function that receives 1 int and returns a null value... Orz.

Determination of member functions

Spiral casting does not give a decision in the case of const, but because const modifies the element next to its left by default, it modifies the element to the left if there is no element to the right. So you can still use the spiral rule just by looking at const and the elements it modifies as a whole.

Consider the following statements:


const int*const Method3(const int*const&) const;

When a function is followed by an const, it means that the member function's scope *this is constant, meaning that the class entity cannot be modified within the function body.

The following is the analysis of the above statement:

Starting from Method3, encounter (, so Method3 is a function that receives a reference as an input & Part. The reference is of type const int*const, a constant pointer to an orthopedic constant. Continue to encounter *const, so the function returns a constant pointer. The pointer points to the const int orthopedic constant. const at the end of the function, as mentioned earlier, identifies instances that are not modified in the body of the function.

The related resources

The ``Clockwise/Spiral Rule'' The C++ 'const' Declaration: Why & How

conclusion


Related articles: