C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources] - نسخه متنی

Herb Sutter, Andrei Alexandrescu

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید


Discussion


It's hard to find language that's colorful enough to describe macros, but we'll try. To quote from [Sutter04] §31:Stroustrup94] §3.3.1

Macros are almost never necessary in C++. Use

const

(§5.4) or

enum

(§4.8) to define manifest constants [see Item 15]

,

inline

(§7.1.1) to avoid function-calling overhead [but see Item 8]

,

template

s (Chapter 13) to specify families of functions and types [see Items 64 through 67]

, and

namespace

s (§8.2) to avoid name clashes [see Items 57 through 59].

[Stroustrup00] §1.6.1

The first rule about macros is: Don't use them unless you have to. Almost every macro demonstrates a flaw in the programming language, in the program, or in the programmer.

[Stroustrup00] §7.8

The main problem with C++ macros is that they seem much better at what they do than they really are. Macros ignore scopes, ignore the type system, ignore all other language features and rules, and hijack the symbols they

#define for the remainder of a file. Macro invocations look like symbols or function calls, but are neither. Macros are not "hygienic," meaning that they can expand to significantly and surprisingly different things depending on the context in which they are used. The text substitution that macros perform makes writing even remotely proper macros a black art whose mastery is as unrewarding as it is tedious.Exceptions), never ever even consider starting to think about writing a macro that is a common word or abbreviation. Do

#undef ine macros as soon as possible, always give them

SCREAMING_UPPERCASE_AND_UGLY names, and avoid putting them in headers.


/ 521