Aussie AI

Declare Objects with Full Initialization

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Declare Objects with Full Initialization

Another consideration is that objects should not be declared until there is enough information to construct them fully. For example, given a user-defined class “complex”, consider the following code:

    complex c; // construct c
    // ....
    c = 1.0; // initialize c

This is less efficient than calling the correct constructor directly by using:

    complex c(1.0); // construct and initialize c

The first code sequence involves a call to the default constructor and the overloaded operator=, whereas the second declaration calls only the (double) constructor for the complex class.

Unfortunately, there are practical limits to the extent to which objects can be declared near their first use. If the first use of an object is inside a compound statement, and the object must also be used outside the compound statement, the scope resolution rules prevent the declaration from being placed inside the compound statement. For example, consider the code below:

    double d;
    complex c;
    while(....) {
        cin >> d; // get double value from user
        c=d; // set complex number
    }
    cout << c; // print the complex number

In this sequence, it would be more efficient to declare “c” inside the loop block using the direct call to a double constructor:

    complex c(d);

However, this would prevent the use of c outside the scope of the braces. This limitation is an unfortunate consequence of the programming language design choice to make braces both the method of grouping statements and the scoping mechanism in C++ (but there are many more important advantages supporting this decision). Unfortunately, it is not even possible to remove the braces in the above example, using the comma operator as by:

    while(....)
        cin >> d, complex c(d); // FAILS: compilation error

C++ syntax prevents a declaration from being an operand of the comma operator.

Nothing Constructors. What we really want is a way to declare a class type variable, but not run its constructor. I'm not aware of a good way to do this. One way would be to use pointers and dynamically allocated “complex” objects, which is successful and standardized, but this adds extra memory management overhead.

Here's a thought. Maybe something like this works? Declare a dummy constructor with a dummy parameter type:

    class Banana { };
    complex(Banana b) {  } // nothing!

Then your call to the dummy constructor is hopefully optimized to nothing:

    Banana b;
    complex c(b);  // Nothing!

 

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++