Specifying Integer Size
Sometimes you know that you’re only going to need a number that goes up to 255. It doesn’t make sense to create a full
int since that is typically encoded as 4-bytes large. A single byte is capable of coding numbers up to
C++ allow you to specifically declare the size of your integer, with the following syntax.
You’re technically using some of these without even knowing it. Assuming you’re running
c++ on a 64-bit system, these following types are equivalent:
int8_t, an unsigned 1-byte integer
int16_t, a signed 2-byte integer
int32_t, a signed 4-byte integer
int64_t, a signed 8-byte integer
uint64_t, an unsigned 8-byte integer
C++ Style Guide
If you develop software on a MacOS, you might want to use the style guide, which is compatible with
clang’s auto-formatting capabilities.
The style guide notes outlined below are taken directly from Apple’s
llvm and outline some of the details I found to be the most noteworthy.
#include <iostream>unless you really need more functionality than
<stdio.h>is capable of providing with its trusty
using namespace std. It pollutes the global namespace and can cause collisions which are frustrating to debug
std::endlas a generic substitute for
\n. The command
os.put('\n')and then calls
os.flush()which is likely more computationally intensive than you desire.
Avoid using post-increment operators. Favor using pre-increment operators. They are computationally faster and save you from painful debugging later on.
Avoid throwing exceptions. Most exceptions can be avoided with good programming practices, and they are computationally expensive to handle.
When coding in
C++, favor using
std::static_cast<T>(var)over C-style casts, (e.g.
GNU C Compilers
gccis the Gnu C compiler
g++is the Gnu C++ compiler
However, with this being said,
gcc is a fully functional C++ compiler, and
g++ is effectively just a mapping to
gcc -xc++ -lstdc++ -shared-libgcc
Setting the Version
g++ --std=c++1z # (ISO c++ standard) g++ --std=gnu++1z # (GNU c++ standard)
Optimize compilation for the Raspberry Pi 4
export CFLAGS="-march=cortex-a72 -mcpu=cortex-a72 -mfloat-abi=hard -mfpu=neon-fp-armv8 -mneon-for-64bits"
gcc -march=native -Q --help=target | grep -- '-march=' | cut -f3 | head -n 1 # on macOS: 'haswell' # on AWS EC2: 'skylake-avx512' # on Raspberry Pi: 'cortex-a72'