Moos Hueting Moos Hueting - 1 year ago 74
C++ Question

Typedef in traits vs typedef in class

I'm looking through the Eigen source code for educational purposes. I've noticed that for each concrete class template

in the hierarchy, there is an
defined. A typical example can be found in Matrix.h:

namespace internal {
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
typedef _Scalar Scalar;
typedef Dense StorageKind;
typedef DenseIndex Index;
typedef MatrixXpr XprKind;
enum {
RowsAtCompileTime = _Rows,
ColsAtCompileTime = _Cols,
MaxRowsAtCompileTime = _MaxRows,
MaxColsAtCompileTime = _MaxCols,
Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
CoeffReadCost = NumTraits<Scalar>::ReadCost,
Options = _Options,
InnerStrideAtCompileTime = 1,
OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime

Now I understand traits to be a way of extending existing classes that you do not want to modify with extra information pertaining to some piece of new code. For example, a user of class template
Foo<class TAllocator>
might want to make use of existing memory allocators
, but Foo needs to know how to interface with these two, and as such a
are defined by the user, which in turn are used by

In this case, however, I don't readily see the problem with just specifying
in each derived class, i.e. have
using a typedef in the class body. What is the advantage here of using a traits class? Is it just for the purposes of keeping the code clean, i.e. storing all relevant properties of each class in the traits class?

Edit as per Nicol Bolas's response: I understand that some of these typedefs might need to be kept "internal", i.e. should not be exposed to the user, which would explain the traits class. That seems to make sense, however some of these typedefs, such as
, are available to the outside world, through a typedef in the base class of

template<typename Derived> class MatrixBase
: public DenseBase<Derived>

typedef MatrixBase StorageBaseType;
typedef typename internal::traits<Derived>::StorageKind StorageKind;
typedef typename internal::traits<Derived>::Index Index;
typedef typename internal::traits<Derived>::Scalar Scalar;
typedef typename internal::packet_traits<Scalar>::type PacketScalar;
typedef typename NumTraits<Scalar>::Real RealScalar;

This brings us back to the original question: why isn't
just a typedef in
itself? Is there any reason aside from stylistic choice?

Answer Source

I suspect that, since the traits class is internal, that this is the point of using a traits class. That is, to keep these things internal. That way, Matrix doesn't have a lot of oddball definitions and such, even in its private interface.

Consider the enumeration in your example. Those "enums" (aka: static constexpr variables before C++11) don't look like anything that a user should know about. It's an implementation detail, and therefore it should be hidden.

MatrixBase's problem is a CRTP issue.

See, Matrix would be defined like this:

class Matrix : public MatrixBase<Matrix>

This partial definition causes 2 things to happen:

  1. If Matrix has not already been declared as a class type, then it becomes a legal class who's name can be referenced and used.

  2. The template MatrixBase must be instantiated with the type Matrix. Right now.

The problem here is that "right now", Matrix is an incomplete class. The compiler has not yet entered the body of that definition, so the compiler doesn't know anything about its internals. But MatrixBase must be instantiated right now.

Therefore, MatrixBase cannot use any of the contents of the Derived class it is provided. If Matrix has some typedef in it, MatrixBase<Derived> cannot see it.

Now, member functions of MatrixBase<Derived> can look at definitions in Derived, because those are defined after the full class is defined. Even if those functions are defined within the scope of the class.

But you can't have properties of MatrixBase access properties of Derived. Hence the traits indirection. The traits class can use a specialization based on an incomplete type to expose defines to MatrixBase.