dynamic_cast<std::integer>(C)++

March 29, 2010

The C++ Standard Committee has been hard at work defining the next version of the language. Here’s a provisional list of changes to be included:

  1. For consistency with strings, integers have been split into built-in constants of type int (3, 100, 0xFF, etc.) and the std::integer class. The class has operator overloading to make math easy. The built-in int type no longer has these operators. Constants must be cast to the class for the operators to work:

    Before:  5 + 6
    After:   std::integer(5) + 6
    

    Note that the 6 does not have to be cast because the + operator is overloaded to take integer constants, conveniently casting them for you. This new consistency between integers (and floats, doubles, char, bool, etc.) and strings will reduce confusion.

  2. Functions often want to return pointers to local variables, or pass pointers to local variables to other functions. This isn’t safe because when the stack is unwound, those pointers are no longer valid. This has been fixed in the new standard by extending the lifetime of local variables indefinitely. The programmer is responsible for releasing the memory explicitly. For example:

    int add(int a, int b)
    {
        std::integer sum = std::integer(a) + b;
    
        delete &a;
        delete &b;
    
        int sum_value = sum.c_int();
    
        delete &sum;
    
        return sum_value;
    }
    

    The local variable sum was explicitly freed. Note that the parameters also had to be freed. Did you spot the bug? The local variable sum_value was returned (by value), but its address was never freed, resulting in a memory leak. APIs will have to be carefully documented to specify whose responsibility it is (caller or callee) to free both passed-in parameters and returned values. Addresses can be returned by reference parameters to pointers to variables, but remember to free those pointers too.

  3. Current C++ compilers must maintain complicated data structures of the functions that are declared or used in each source file, in order to generate the appropriate binary object files. In order to reduce this burden on the compiler, footer files have been introduced. These are files that are included at the bottom of a .cpp file, which summarize all functions that are defined or used by that file. The format is somewhat different than a traditional .hpp file (because the needs of the compiler are different there). The standard recommends the extension .fpp for declaring these methods and their signatures.

  4. To remove the confusion over the distinction between the NULL constant, the 0 constant (for pointers), and C++0x’s nullptr constant, and to increase type safety, the null constant is now a template:

    MyClass *ptr = std::pointer_constants<MyClass *>::null();
    
  5. Implementing the Singleton Pattern (Gamma et al) requires writing the boilerplate method to create a single instance. To facilitate this common pattern, the language introduces the keyword old. The program should create one instance of the object at start-up:

    MyClass *ptr = new MyClass;
    

    and other methods can get a reference to that instance using the following construct:

    MyClass *ptr = old MyClass;
    

    If there are several instances of MyClass in the heap, it is undefined which one is chosen. If there are no instances, a null reference is returned (see previous item). Be careful to not accidentally get a reference to an object on the stack.

  6. To reflect the enhancements to the language since 1983, the name of the language has been changed to: dynamic_cast<std::integer>(C)++.

(With Drew Olbrich)