Snap! Websites
An Open Source CMS System in C++
The following gives you the level of all C++ operators. It is often useful to know.
| Level | Operator | Description | Grouping |
|---|---|---|---|
| 1 | :: | scope | Left-to-right |
| 2 | a() a[] a.b a->b a++ a-- dynamic_cast static_cast reinterpret_cast const_cast typeid type() type{} | postfix | Left-to-right |
| 3 | ++a --a ~a !a sizeof new new[] delete delete[] co_await | unary (prefix) | Right-to-left |
| *a &a | indirection and reference (pointers) | ||
| +a -a | unary sign operator | ||
| (type) | type casting | Right-to-left | |
| 4 | .* ->* | pointer-to-member | Left-to-right |
| 5 | a*b a/b a%b | multiplicative | Left-to-right |
| 6 | a+b a-b | additive | Left-to-right |
| 7 | << >> | shift | Left-to-right |
| 8 | <=> | three-way comparison | Left-to-right |
| 9 | < > <= >= | relational | Left-to-right |
| 10 | == != | equality | Left-to-right |
| 11 | a&b | bitwise AND | Left-to-right |
| 12 | ^ | bitwise XOR | Left-to-right |
| 13 | | | bitwise OR | Left-to-right |
| 14 | && | logical AND | Left-to-right |
| 15 | || | logical OR | Left-to-right |
| 16 | ?: throw co_yield | conditional | Right-to-left |
| 17 | = *= /= %= += -= >>= <<= &= ^= |= | assignment | Right-to-left |
| 18 | , | comma | Left-to-right |
Note: post increment and decrement uses a dummy int parameter: T::operator ++ (int) for t++;.
The min and max operators, which were depracated around version 4.0 of gcc/g++ had the same priority as the relational operators (so priority of 9 in my table above.)
The following are the standard and not so standard C/C++ escape characters supported in strings.
| Sequence | Character | Name |
|---|---|---|
| \a | 0x07 / 7 | Bell or alert |
| \b | 0x08 / 8 | Backspace |
| \f | 0x0C / 12 | Formfeed |
| \n | 0x0A / 10 | Newline (next line) |
| \r | 0x0D / 13 | Carriage Return (beginning of line) |
| \t | 0x09 / 9 | Horizontal Tab |
| \v | 0x0B / 11 | Vertical Tab |
| \' | 0x27 / 39 | Single Quote |
| \" | 0x22 / 34 | Double Quote |
| \\ | 0x5C / 92 | Backslash |
| \? | 0x3F / 63 | Literal Question Mark |
| \0 | 0x00 / 0 | Null |
| \OOO | 0x## / # | Octal Notation Character |
| \xHH | 0x## / # | Hexadecimal Notation Character (char) |
| \xHHHH | 0x#### / # | Hexadecimal Notation Character (wchar_t) |
In general, for a character c, \c will represent c if not otherwise defined in this table.
A backslah followed by a newline is used to continue a line in preprocessor code.
Once in a while, we make use of a function found in the C library (and at times even other libraries) which return a pointer to an object that needs to be released one we are done with it.
The easiest way to do that with C++ and especially RAII (in case you may get an exception before you reach the line were you would otherwise free the resource) you can use a shared pointer in this way (example for using getifaddrs() as found in snapcommunicator.cpp at this point):
namespace
{
void ifaddrs_deleter(struct ifaddrs * ptr)
{
freeifaddrs(ptr);
}
} // no name namespace
...
{
struct ifaddrs * if_ptr;
if(getifaddrs(&if_ptr) == 0)
{
std::shared_ptr<struct ifaddrs> safe_if_ptr(if_ptr, ifaddrs_deleter);
... // do work here, if exception or return, if_ptr gets freed
}
// else -- handle error case
}
...
You may use a lambda function, just know that there are issue in doing so. I find it easier to have an explicit deleter like here. It is pretty nice and simple to use.
Snap! Websites
An Open Source CMS System in C++