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. inlinefunctions 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.0fisfloat, whereas0.0isdouble). - Persistent
staticlocal variables inside functions (often dangerous, but occasionally useful). - Unsigned constants with “
u” suffix (e.g.1uisunsigned intversus1isint). __FILE__and__LINE__builtin preprocessor macros for source code filename and line number.- Operator overloading of
newanddeleteto 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::bitsetlibrary 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_assertfor 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_castfor fixing tricky casts that should be illegal, but you need what you need.- The
__func__builtin preprocessor macro for source code function name. std::backtracelibrary for function call stack reporting (in C++23).- GCC compiler non-standard
#pragmadirectives such as:#pragma GCC unroll N - Standard C++ random number generator libraries. The older
randandsrandfunctions are deprecated. - The
registerkeyword is officially deprecated/removed, since C++17. Compilers don't need your help! - Binary numeric literals with
0bprefix and%bprintfbinary format specifier (since C++14). Works similarly to0xand%xhexadecimals. - 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++ |