Classes | Public Member Functions | List of all members
PLMD::MatrixSquareBracketsAccess< T, C, I, J > Class Template Reference

Utility class to add [][] access. More...

#include <MatrixSquareBracketsAccess.h>

Inheritance diagram for PLMD::MatrixSquareBracketsAccess< T, C, I, J >:
Inheritance graph
[legend]

Classes

class  Const_row
 Small utility class which just contains a pointer to the T and the row number. More...
 
class  Row
 Small utility class which just contains a pointer to the T and the row number. More...
 

Public Member Functions

Row operator[] (I i)
 access element (with [][] syntax) More...
 
Const_row operator[] (I i) const
 access element (with [][] syntax) More...
 

Detailed Description

template<class T, class C, class I = unsigned, class J = unsigned>
class PLMD::MatrixSquareBracketsAccess< T, C, I, J >

Utility class to add [][] access.

Template Parameters
TThe type of the matrix class.
CThe type of the returned value.
IThe type of the first index (default unsigned).
JThe type of the second index (default unsigned).

It implements the trick described in C++ FAQ 13.12 to allow [][] access to matrix-like classes, based on the (,) syntax, thus translating [i][j] into (i,j). In practice, one only needs to implement the (,) syntax and to inherit from MatrixSquareBracketsAccess. The first template parameter (T) should be the class itself, the second (C) is the type of the returned value, and the third (I) and fourth (J) are the types of the two indexes (unsigned by default). As everything is inlined, no overhead is expected.

class MyMatrixClass:
  public MatrixSquareBracketsAccess<MyMatrixClass,double>
{
  double data[16];
public:
  double & operator ()(unsigned i,unsigned j){
    return data[4*i+j];
  }
  const double & operator ()(unsigned i,unsigned j)const{
    return data[4*i+j];
  }
};

int main(){
  MyMatrixClass m;
  m[0][1]=3.0;
  return 0;
}

Member Function Documentation

◆ operator[]() [1/2]

template<class T , class C , class I , class J >
MatrixSquareBracketsAccess< T, C, I, J >::Row PLMD::MatrixSquareBracketsAccess< T, C, I, J >::operator[] ( i)

access element (with [][] syntax)

◆ operator[]() [2/2]

template<class T , class C , class I , class J >
MatrixSquareBracketsAccess< T, C, I, J >::Const_row PLMD::MatrixSquareBracketsAccess< T, C, I, J >::operator[] ( i) const

access element (with [][] syntax)


The documentation for this class was generated from the following file: