C++ Keywords
C++ keywords before C++11:
- types:
bool,true,falsefloat,doubleshort,int,long,char,unsigned,signed
asmautocompl: as an alternative for~const- type casting:
const_cast,dynamic_cast,reinterpret_cast,static_cast defaultenumexplicitexportgotoinlinemutablenamespacenew,deleteand,and_eq,or,or_eq,not,not_eq,xor,xor_eq,bitand,bitoroperatorpublic,protected,private,friendregisterreturnsizeofstatic,externclass,struct,uniontemplatethistry,catch,throwtypedeftypeidtypenameusingvirtualvoidvolatilewchar_tfor,while,do,break,continueif,else,case,switch
New keywords since C++11
alignasalignofchar16_t,char32_tconstexprdecltypenoexceptnullptrstatic_assertthread_local
New keywords since C++20
char8_tconceptconsteval,constinitco_await,co_return,co_yieldrequires
Identifiers with special meaning
May be used as names of objects or functions, but have special meaning in certain contexts.
final(C++11)override(C++11)import(C++20)module(C++20)
inline
inline: multiple definitions are permitted, originally a specifier for functions, but extended to variables in C++17.
- A function declared
constexpris implicitly aninlinefunction. - A deleted function is implicitly an
inlinefunction.
Best Practices
- do NOT use
inlinefor functions in.ccfiles. - use
inlinein.hfiles when defining non-template functions outside of aclassorstruct.
static
- zero-initialized static data goes in
.BSS(Block Started by Symbol). - non-zero-initialized data goes in
.DATA
static vs extern:
static: internal linkageextern: external linkage
static variables can be defined within a function and will be initialized when they are first used.
thread_local
thread_local Foo foo = ...;
Such a variable is actually a collection of objects, such that when different threads refer to foo they are actually accessing different objects.
thread_local is implicitly static, so thread_local = static thread_local.
noexcept
Since C++11
Compile-time check, specifies that the function will NOT throw any exceptions.
Unlike throw(), noexcept does not require the compiler to introduce code to check whether an exception is thrown. Rather, if a function specified as noexcept is exited via an exception, the result is a call to std::terminate().
friend
Use cases:
- have a
FooBuilderclass be a friend ofFooso that it can construct the inner state ofFoocorrectly, without exposing this state to the world. - may be useful to make a unittest class a friend of the class it tests.
inline
inline functions: to reduce the function call overhead. Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.
Inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining
In theory, functions defined in-class are inlined, but a virtual function / destructor and cannot be inlined.
static constexpr member variables are implicitly inline