Aussie AI
Compilation Problems
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Compilation Problems
C++ has been standardized for decades, or it seems like that. So, I feel like it should be easier to get C++ code to compile. And yet, I find myself sometimes spending an hour or two getting past a few darn compiler errors. Most compilers have a treat-warnings-as-errors mode. Come on, I want the reverse.
Some of the main issues that will have a C++ program compile on one C++ compiler (e.g. MSVS) but not on another (e.g. GCC) include:
const
correctness- Permissive versus non-permissive modes
- Pointer type casting
const correctness refer to the careful use of “const
” to mark
not just named constants,
but also all unchanging read-only data types.
If it's “const
” then it cannot be changed;
if it's non-const
, then it's writable.
People have different levels of feelings about whether this is a good idea.
There are the fastidious Vogon-relative rule-followers who want it, and the
normal reasonable pragmatic people who don't.
Can you see which side I'm on?
Anyway, to get non-const
-correct code (i.e. mine) to compile on GCC or MSVS,
you need to turn off the fussy modes.
On MSVS, there's a “permissive” flag in “Conformance Mode” in Project Settings that you have to turn off.
Pointer type casting is another issue.
C++ for AI has a lot of problems with pointer types,
mainly because C++ standardizers back in the 1990s neglected to create a “short float
” 16-bit
floating-point type.
Theoretically, you're not supposed to cast between different pointer types,
like “int*
” and “char*
”.
And theoretically, you're supposed to use “void*
” for generic addresses,
rather than “char*
” or “unsigned char*
”.
But, you know, this is AI, so them rules is made to be broken,
and the C++ standardizer committees finally admitted as much when
they created the various special types of casts about 20 years later (i.e., reinterpret_cast
).
Anyway, the strategies for getting a non-compiling pointer cast to work include:
- Just casting it to whatever you want.
- Turning on permissive mode
- Casting it to void* and back again (i.e. “
x=*(int*)(void*)(char*)&c
”) - Using “
reinterpret_cast
” like a Goody Two-Shoes.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |