[Valid tive C ++] Clause 02: replace const, Enum, and inline as much as possible # define

Source: Internet
Author: User

Prefer consts, enums, and inlines to # defines.

We often use Preprocessor statements when learning C language, such as # define, # include, # ifdef, # endif. the pre-processing statement cannot be seen by the compiler, so it cannot get a valid error message. for example:

# Define aspect_ratio 1.653

The mark name aspect_ratio may not be included in the symbol table. so when you use this constant to get a compilation error message, the error message will mention 1.653 instead of aspect_ratio. If aspect_ratio is defined in a non-self-written header file, in this way, we need to spend time tracking errors. but do you really need to spend the time to correct the mistakes? In fact, replace # define with const to solve this problem.

Constdouble aspectratio = 1.653;

Because const will be seen by the compiler, and of course it will enter the mark table. The error will be quickly located when the error prompt is displayed.

In addition, const can produce less code than # define, because the pre-processor blindly replaces all aspect_ratio with 1.653, instead of using constants.

 

Constant replacement # Special Cases of defines:

(1) define a constant pointer

(2) Class exclusive constants: scope and static. The scope ensures that constants are limited to class. Static ensures that the constant has at most one entity.

 

In addition, you cannot use # define to create a class-specific constant.

If the compiler does not allow "static integer class constant" to complete "in class Initial Value Setting", you can use the so-called "The Enum hack" compensation method. the theoretical basis is "an enumerative value that can be used to enable ints", for example:

Class gameplayer {

PRIVATE:

Enum {numturns = 5 };

Int scores [numturns];

}

 

Two reasons why Enum hack is worth recognizing:

(1) The behavior of Enum hack is more like # define than Const. for example, it is valid to act on the const constant, but it is invalid to act on Enum and # define.

(2) pragmatism.

 

# Define can be used to implement macros (macros), but macros do have many disadvantages, such:

# Define call_with_max (a, B) f (a)> (B ))? (A) (B ));

When int A = 5, B = 0; call_with_max (++ a, B) is used, call a is accumulated twice. however, macro has some advantages, but it does not incur additional overhead caused by function calls.

To get the efficiency of macros and all the predictable behaviors and type security (Type Safety) of general functions, it is a good choice to replace it with template inline.

And if you want to implement a "private inline function in class" # define is not possible.

 

Summary:

For simple constants, it is best to replace # defines with const or enums.

Replace # defines

 

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.