Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline calls to non-inline base class functions (what does that mean exactly)?

Tags:

c++

templates

I'm reading through a C++ book and I'm in a section about reducing the object code generated by templates (Effective C++ III By Scott Meyers). One of the examples it gives is:

template <typename T, std::size_t n>
class SquareMatrix: private SquareMatrixBase<T> {
    public:
        SquareMatrix()
            : SquareMatrixBase<T>(n, 0),
              pData(new T[n*n])
        { this->setDataPtr(pData.get()); }

        ... functions ...

    private:
        boost::scoped_array<T> pData;
};

where the base class, SquareMatrixBase has a function called:

void invert(std::size_t matrixSize);

"The book moves on to say "Regardless of where the data is stored, the key result from a bloat point of view is that now many -- maybe all -- of SquareMatrix's member functions can be simple inline calls to non-inline base class versions that are shared with all other matrices holding the same type of data, regardless of their size."

What does it mean by "inline calls to non-inline base class versions..."? If its an inline call I would have thought it would have put the whole base class version of any function into the place where inline was used, but this would result in the same code bloat I would have thought. It says this like it's a benefit against code-bloat though.

If you need more background information let me know, the chapters long though and I tried pretty hard to provide background information but I might have missed something.

||EDIT - Additional Information||

The purpose of having Square Matrix and Square matrix base in this passage was:

SquareMatrix was originally a standalone template (not derived). It contained a series of functions which did operations based on the template parameter n's value. So, there was essentially a copy of every function for every value of n used (or for every pair of n, T used) as a new template with those functions was instantiated for each parameter pair. SquareMatrixBase was created to move the functions dependent on the size parameter to the base class. Since the base class is only instantiated with a type parameter (and not the size) the functions in the base class can be called by passing in the value for size the derived class passes into the base class constructor. This means there is only one version of the functions for each typename T passed into the SquareMatrix template regardless of the std::size_t n passed in (as opposed to one version of each function for each combination of {T, n}.

like image 208
John Humphreys Avatar asked Oct 12 '11 14:10

John Humphreys


Video Answer


1 Answers

The point is that SquareMatrix::invert() is inlined, so the function doesn't even appear in the resulting code, and instead the protected base function SquareMatrixBase::invert(n) is called directly.

Now since that function is not inlined, there is only one single instance of that function (for each type T), rather than one copy for each size n. This stands in stark contrast to the single-class design, where one invert() function would be instantiated for each value of n.

like image 132
Kerrek SB Avatar answered Sep 22 '22 16:09

Kerrek SB