User:Dan Polansky/C++ programming

What follows is to complement C++ Programming wikibook.

C-style casting
At a glance:

See also C_Programming/Simple_math.

Links:
 * 3.12 Type Casts, The GNU C Reference Manual, gnu.org

C++-style casting
At a glance:

Above, const cast and dynamic cast are not covered.

Const cast is used to remove constness and more.

Dynamic cast is for pointers and references to instances of polymorphic classes, and enables to treat what is on the face of it available as a broad class as a narrower class. A class is polymorphic if it has at least one virtual method. A failed dynamic cast of a pointer returns null. An example:

See also C++_Programming/Programming_Languages/C++/Code/Statements/Variables/Type_Casting.

Links:
 * Casting Operators, microsoft.com
 * Working Draft, Standard for Programming Language C++, 28 February 2011, open-std.org, search for "5.4 Explicit type conversion (cast notation)"

Zero initialization
What follows is tentative. Some variables and attributes are automatically initialized to zero. These are above all those with static storage duration. Objects with static storage duration include the following: These objects are stored neither on the stack nor on the heap. Static storage duration is not to be confused with static variable, declared so using the keyword static; a non-static global variable has static storage duration. Static storage duration stands in contrast with automatic storage duration (stack) and dynamic storage duration (heap).
 * Global variables
 * Static variables outside of functions, global to a compilation unit
 * Static variables inside of functions
 * Static class attributes

The above may hold only for plain old data (POD); to be clarified.

The relevant part of the C++03 standard is section 3.6.2 Initialization of non-local objects.

Links:
 * Working Draft, Standard for Programming Language C++, 28 February 2011, open-std.org, search for "3.6.2 Initialization of non-local variables"
 * C++, wikipedia.org
 * zero initialization and static initialization of local scope static variable, stackoverflow.com
 * Storage Duration, cs.kent.edu/~durand

Plain old data
As a first approximation, plain old data (POD) is C data as opposed to C++ data. This includes ints, floats, pointers, C structs, and arrays. An array of non-POD types is not plain old data. However, certain classes are plain old data as well. A struct or class that has a programmer-defined constructor, a destructor, or a virtual function is not plain old data; some other cases are not plain old data as well. C++11 offers std::is_pod template to find out, requiring #include . Links:
 * C++ Language Note: POD Types by Walter E. Brown, 1999, fnal.gov
 * Working Draft, Standard for Programming Language C++, 28 February 2011, open-std.org, search for "9 Classes", and there paragraph 9 starting with "is a class that is both a trivial class and a standard-layout class"
 * What are POD types in C++?, stackoverflow.com

Constructor call order
In sum, the body of the base class constructor first. A constructor of a derived class calls the constructors of its base classes before it starts executing its own body.

Thus, if class AppleTree derives from Tree, which derives from Plant, which derives from Thing, the order of executing the class constructor bodies upon instantiation of AppleTree will be this: Note that the constructor invocation order is the opposite of the above. Thus, a contrast needs to be made between constructor invocation order and constructor body execution order. AppleTree is invoked first, and the very first thing it does is invoke Tree constructor, which invokes Plant constructor as the very first thing, which invokes Thing constructor as the first thing. Thing constructor handles its initializer list and executes its body, and then returns control to Plant constructor. Plant constructor handles its initializer list and then its body, and returns control to Tree constructor; etc.
 * Thing
 * Plant
 * Tree
 * AppleTree

For multiple inheritance, the picture is a little more complex: the multiple base classes are taken in the order of derivation, from left to right.

Links:
 * Order of calling constructors/destructors in inheritance, stackoverflow.com
 * Constructors (C++) # Order of Construction, Visual Studio 2015, msdn.microsoft.com
 * Order of constructor & destructor calls in Bruce Eckel, Thinking in C++, linuxtopia.org

Template compilation
Template definitions have to be included (usually via #include) in the compilation units that use them, and thus are often placed into header files. The result may be that the compiler creates a duplication of template executable code in .o files of the separate compilation units that use a particular template instantiation, a duplication that is only removed by the linker. Some compiler suites use compilation strategies to avoid that duplication.

A technique called explicit instantiation can be used by the programmer to eliminate that duplication, generally resulting in reduced compilation time. It consists in marking one or more compilation units for inclusion of a particular template instantiation while marking other compilation units that use that instantion for exclusion and mere referencing of the code of that instantiation. The latter is ensured by adding the keyword extern to the explicit instantiation syntax. Thus, the compilation units in which an instantiation is marked for exclusion depend on other compilation units for this to work at the linking time.

Possible good location for this material: C++ Programming/Templates.

Links:
 * An Introduction to C++ Templates by Kais Dukes, 2002, codeguru.com
 * Template compilation models in Bruce Eckel, Thinking in C++, linuxtopia.org
 * Working Draft, Standard for Programming Language C++, 28 February 2011, open-std.org - section 14.7.2 Explicit instantiation
 * Moving Templates Out of Header Files, 1997, drdobbs.com
 * 7.5 Where's the Template? in Using the GNU Compiler Collection (GCC), gcc.gnu.org
 * Explicit Instantiation, Visual Studio 2015, msdn.microsoft.com
 * Chapter 7 Compiling Templates in Sun Studio 12: C++ User's Guide, docs.oracle.com
 * C++
 * Template (C++)

Interpreter
There exist approximate interpreters of C and C++, allowing interactive entry as if in a command shell.

Cint is one such interpreter, licensed under X11/MIT license. It can be currently downloaded from hanno.jp. Limitations of Cint are documented at cern.ch.

To use Cint, run it, and on its command line enter. (period) and press enter. Thereafter, you can have an interactive session: cint.exe> mystr = "Hello" // ; is optional and the type is auto (const char* 0x3ecdb8)"Hello" cint.exe> mystr[0] (char 72)'H' cint.exe> *(mystr+1) (char 101)'e' cint.exe> sizeof(mystr) (const int)4 cint.exe> float pi = 3.141592 cint.exe> *(int*)&pi (int)1078530008 cint.exe> printf("pi cast %x\n", *(int*)&pi) pi cast 40490fd8 (const int)17 cint.exe> puts("Hey") Hey (const int)0 cint.exe> int myarray[] = {1,2,3} cint.exe> myarray[1] (int)2 cint.exe> int nested[2][2] = {{1,2},{3,4}} cint.exe> nested[1][1] (int)4 cint.exe> for (int i=0;i<2;i++) { printf("%i\n",i); } 0 1 cint.exe> sin(4.0) // Some math from math.h but not all of it (const double)(-7.56802495307928200e-001) cint.exe> strlen("abc") // Some string functions (const unsigned int)3

Naming conventions
Different naming conventions for variables, types, etc. are used by different organizations.

See also C++ Programming/Weblinks.

Links:
 * GCC Coding Conventions, gcc.gnu.org
 * Google C++ Style Guide, google.github.io
 * LLVM Coding Standards, llvm.org
 * Joint Strike Fighter Air Vehicle C++ Coding Standards, stroustrup.com
 * Mozilla Coding Style, developer.mozilla.org

Brace conventions
Some put the opening brace ({) on a separate line, some don't:
 * The K&R style seems to place the opening brace on a separate line for functions but not for control structures: Indentation style.
 * GCC Coding Conventions do not seem to be explicit bout this, but code examples they provide place the opening brace of a function definition on a separate line. An example file is fixed-bit.c at github.com.
 * Google C++ Style Guide says that "The open curly brace is always on the end of the last line of the function declaration, not the start of the next line."
 * LLVM Coding Standards do not seem to be explicit about this, but code examples they provide do not place the opening brace of a function definition on a separate line. Example file: LLLexer.cpp at github.com.
 * Joint Strike Fighter Air Vehicle C++ Coding Standards place the braces on separate lines as per AV Rule 60.
 * Mozilla Coding Style indicates that Firefox uses Google Coding style for C++ code. As an example, nsBrowserApp.cpp at dxr.mozilla.org uses multiple styles.
 * Linux code base: fork.c at github.com places the opening bracket on a new line for functions but on the same line for control structures such as if.
 * Python C implementation: ceval.c at github.com seems to use the K&R style: opening brace on a new line for functions but not for control structures. This seems to match PEP 7 (Style Guide for C Code).
 * C Style and Coding Standards for SunOS from 1993 at cis.upenn.edu seem to use the K&R style.

Links:
 * See the links in section.
 * Indentation style