Aussie AI
New C++ Language Features
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
New C++ Language Features
Here's a summary of some C++ language features that are relevant to AI development. The longstanding features of C++ include:
- Bitwise operators:
&
(bitwise-and),|
(bitwise-or),^
(bitwise-xor),~
(bitwise two's complement), since language inception. inline
functions are fast (and it's actually true these days).- Short-circuiting of the
&&
and||
operators is standard. Use it and abuse it. - The
?:
ternary operator also short-circuits. - String literal concatenation of adjacent string constants (e.g.
"a" "b"
becomes"ab"
). - Float versions of standard math functions (e.g.
sqrtf
,expf
,logf
, etc.) - Float versions of numeric constants (e.g.
0.0f
isfloat
, whereas0.0
isdouble
). - Persistent
static
local variables inside functions (often dangerous, but occasionally useful). - Unsigned constants with “
u
” suffix (e.g.1u
isunsigned int
versus1
isint
). __FILE__
and__LINE__
builtin preprocessor macros for source code filename and line number.- Operator overloading of
new
anddelete
to intercept them at link-time. - Variable-argument function definitions using
va_start
,va_arg
, andva_end
.
Recently Added C++ Features
The C++ language has evolved over many years, and has had a great many features added to it. The newer features of C++ standardization that may be useful to know:
std::bitset
library for bit sets and bit vectors.- Builtin functions for MSVS and GCC compilers, including the intrinsic functions with access to machine-level instructions (e.g. the x86 instruction set).
- The “
restrict
” keyword for pointers (indicates non-aliasing, for better compiler auto-optimization). This feature has been evolving over the years and there are various earlier language extensions with different keywords. static_assert
for compile-time assertions of constant expressions that trigger compiler errors.- Variable-argument preprocessor macros with the ellipsis “
...
” and__VA_ARG__
tokens (since C++14), such as to define your own debug macro version ofprintf
. There's also the__VA_OPT__(args)
method since C++20 for fixing some obscure syntax problems with using vararg macros. reinterpret_cast
for fixing tricky casts that should be illegal, but you need what you need.- The
__func__
builtin preprocessor macro for source code function name. std::backtrace
library for function call stack reporting (in C++23).- GCC compiler non-standard
#pragma
directives such as:#pragma GCC unroll N
- Standard C++ random number generator libraries. The older
rand
andsrand
functions are deprecated. - The
register
keyword is officially deprecated/removed, since C++17. Compilers don't need your help! - Binary numeric literals with
0b
prefix and%b
printf
binary format specifier (since C++14). Works similarly to0x
and%x
hexadecimals. - The
<=>
three-way comparison (spaceship) operator. My favorite one.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |