Programming‎ > ‎Language‎ > ‎C++‎ > ‎

Keywords

The Bird's the Word

This is my notes on some of the reserved keywords in C++. Look at cppreference.com for a full list.

Fundamental types
void
Used to create pointers to void (any type) and to indicate lack of return value and lack of arguments to functions.

nullptr (C++11)
Use instead of 0 or NULL.

bool - false / true
Type that holds one of the two values: true or false. They are also keywords. The value of a sizeof(bool) is implementation defined and might be different from 1.

char / wchar_t and (since C++11) char16_t / char32_t

int
Create a integer type.
  • signed / unsigned : These keywords select if it can be positive/negative or positive values only.
  • short / long / long long: Size of the int and how big values it can contain. Implementation defined.

float / double
Creates a floating point type, usually of the IEEE-754 form.

auto (C++11)
Specify that the variable that is being declared should be automatically deduced from it's initializer. It can be used to create a iterator to loop the content of an STL container for example.




Loop constructs
for
Range-based for loops

do

while



break

continue

Conditional statements
if


else
switch, case, default(1)

User-defined types



enum
Declares an enumaration, a distict type that consist of a set of namned constants. Using enum only creates an unscoped enumeration.
enum class (C++11)
Scoped enumerations do not put the their enumerators in the surrounding scope.


class


typedef



access specifiers
private
protected
public

friend


Member function specifier
virtual
Specify that a non-static member function is virtual. It will support dynamic binding and can be overridden in derived classes.


override (C++11)
Specify that a virtual functions overrides another virtual function.

Final (C++11)
Specify that a virtual function cannot be overridden or that a class cannot be inherited from.


= delete (C++11)
= defaulted (C++11)

explicit

inline
C++ Keyword inline and .inl Files - 2015

cast
dynamic_cast<>
reinterpret_cast<>

static_cast<>
Converts Y to X and return it. No checks are made that the cast is valid. If A is not related to B in any way using the pointer returned is undefined.

const_cast<>
To modify the constness of a object pointed to by a pointer const_cast is used. It can be used to set or remove const from the object. Useful when using api's that can not be modified so one needs to send in a non const pointer.

Run-time type information
RTTI is a way to allow the type of on object to be determined when the program is running.

typeid

Exceptions

try
catch
throw
noexcept (since C++11)

Top 15 C++ Exception handling mistakes and how to avoid them - 2016
type qualifiers
const
Const is used to inform the compilers that something should not be allowed to change. It can be a variable, arguments to a function or a member function. The main use of const is to prevent programming mistakes. Const is like the Borg so when you start adding it to you code you will need to add it everywhere where it's needed.

http://www.cprogramming.com/tutorial/const_correctness.html

volatile
Tells the compiler that every access (read or write operation, member call, etc) to the object must be performed and can not be optimized away even if it looks like the action is reduntant. It can be used when accessing memory ports to hardware that do not act like normal memory.

mutable
Used on non-static and non-const data members of a class this tells the compiler that it is legal to assign a value to this data member from a const member function. This can be used on internal data that does not change the externally visible state of the class. One example is a class where advanced calculations are needed to get some form of data about the object. It can then be stored in a mutable variable so it can be updated any time even in a const accessor function.


Memory
delete
new


alignas (C++11)
Specifies the alignment requirement of a type or an object.

alignof (C++11)
Get the alignment requirements of a type, in bytes.

sizeof
Return the size in bytes of the object or type.

Storage
register (deprecated C++11)
Hint's the compiler to place the object in a processor register. Deprecated in C++11 as the compiler does as it wish anyway.

static

extern

thread_local (C++11)


Namespace
namespace


using

Others
asm
constexpr (C++11)
decltype (C++11)
export
goto
typename
this

static_assert (C++11)
Performs an assertion check at compile-time. If true nothing happens, if false the compiler displays the specified error messages. Useful with template code to verify that the template will work with the provided types. The content of the <type_traits> header is useful when doing that.


template
template

Bitwise operation


Comments