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 is float, whereas 0.0 is double).
  • Persistent static local variables inside functions (often dangerous, but occasionally useful).
  • Unsigned constants with “u” suffix (e.g. 1u is unsigned int versus 1 is int).
  • __FILE__ and __LINE__ builtin preprocessor macros for source code filename and line number.
  • Operator overloading of new and delete to intercept them at link-time.
  • Variable-argument function definitions using va_start, va_arg, and va_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 of printf. 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 and srand 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 to 0x and %x hexadecimals.
  • The <=> three-way comparison (spaceship) operator. My favorite one.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++