[C/C ++] uncommon keywords (Reserved Words)

Source: Internet
Author: User

Restrict keywords

The restrict keyword is actually only available in the C language, which is proposed by C99. Currently, C ++ does not support this keyword.

When a pointer is in the declared scope, all data in the memory area pointed by the pointer can only be accessed through it, but not through other pointers. The compiler checks this.

The consequence is to help the compiler optimize code and generate more efficient assembly code.

Currently, only C99 is supported, so the compilation command is as follows:

Gcc-std = c99 test. c-o test. bin

Note: g ++ does not support-std = c99, and does not support the restrict keyword.

 

For example:

Void f (const int * restrict src, int * restrict dst) {* dst = (* src) + 1;} int main () {int n = 0; f (& n, & n); // compilation error. If the restrict keyword is not added, return 0;} can be compiled ;}

Restrict optimization by the compiler

// Since x and y may be equal, the compiler cannot optimize int foo (int * x, int * y) {* x = 0; * y = 1; return * x ;} // because x and y are not equal, the compiler knows that the function must return 0 and may optimize the entire function to return 0; you can even use 0 to replace int foo (int * restrict x, int * restrict y) {* x = 0; * y = 1; return * x ;}

The use of restrict is a "gentleman agreement"

// Because pointer usage is arbitrary, restrict cannot ensure that data is not overwritten. // The following program shows a copy with overlapping memory, which cannot be detected by the compiler. Void mycopy (const void * restrict src, void * restrict dst, int size) {if (size <= 0 | src = NULL | dst = NULL) return; const char * s = (char *) src; char * d = (char *) dst; while (size --) * d ++ = * s ++ ;} int main () {char array [100] = {0}; mycopy (array, array + 10, 50); return 0 ;}

References

Http://blog.sina.com.cn/s/blog_530bce2d0100crxu.html

Http://hi.baidu.com/oney131/blog/item/10f71f1f0b5033174034176e.html

 

Explicit keywords

Block the occurrence of implicit conversions that should not be allowed by the converted constructor. Constructors declared as explicit cannot be used in implicit conversions.

In C ++, a constructor for a parameter (or a multi-parameter constructor with default values for all parameters except the first parameter ),
Assume two roles. 1 is a constructor 2 is a default and implicit type conversion operator.

So,
Sometimes, when we write code such as AAA = XXX, and exactly the XXX type is the parameter type of the Single-parameter constructor of AAA, the compiler automatically calls this constructor, create an AAA object.

Class Test1 {public: Test1 (int n) {num = n ;}// common constructor private: int num ;}; class Test2 {public: explicit Test2 (int n) {num = n ;}// explicit (explicit) constructor private: int num ;}; int main () {Test1 t1 = 12; // call its constructor implicitly, successful Test2 t2 = 12; // compilation error, cannot implicitly call its constructor Test2 t3 (12); // display successful call return 0 ;}

Volatile keywords

The volatile keyword tells the compiler that this variable cannot be optimized and cannot be cached in the register. This value must be reloaded from the memory every time.

This is common in multi-threaded environments, where a variable is public to multiple threads.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.