views:

100

answers:

4

Hi,

I need to generalise a C++ typedef so I do not need to copy and paste a lot of code. I am serializing blitz arrays using boost and I am defining my own load and save methods and need to do this based on template parameters. Basically I don't know how to generalize

typedef blitz::Array<double, 2> my_Matrix; 

for higher order tensors ie, I want to generalise the above so that it will take 1,2,..,n and if possible do the same for the type.

I guess I am chasing something like a typedef template.

Cheers

Mark

+4  A: 

Don't know much about blitz array but you should be able to put the typedef inside a templated struct. E.g. something like:

template<int N>
struct Matrix
{
    typedef blitz::Array<double,N> Type;
};

Use like thus Matrix<3>::Type.

As for your other question regarding serialisation, looking at the documentation for array:

http://www.oonumerics.org/blitz/manual/blitz02.html

it looks like you can just replace the rows * cols test for size with a call to size(). The data() method should still work for matrices of rank > 2.

jon hanson
A: 

C++0x has it:

template <size_t N>
using MyMatrix<N> = blitz::Array<double,N>;

However you'll still need a template method for load and save anyway.

Matthieu M.
A: 

A typedef is really just creating another name for something. It isn't quite like doing a #define, but its close. Being just another name, you can't really do anything with them that you couldn't do with the exact name they are replacing.

What you could do is go all the way and use #define. Like so:

#define my_Matrix(X) blitz::Array<X>

Personally, I hate the preprocessor, and would prefer it be removed from the language.

T.E.D.
A: 

Thanks for the help guys but maybe I should have been a bit clearer

Basically I am lost with the below block of code I got of the net, particularly where the programmer mentions that 'Generalizations to higher-order tensors are trivial'

Ideally I want to keep at least the no. of dimensions general, but ideally the matrix type as well.

Cheers

/**
 * @typedef Defines a matrix type for the example.
 * Generalizations to higher-order tensors are trivial
 */

typedef blitz::Array<double, 2> my_Matrix;

/**
 * Functions to serialize array. The data() member function in Blitz++
 * gives you a pointer to the first element of the array holding the data.
 * Be careful if you use a non-default Blitz++ order.
 *
 * @see
 *  http://www.boost.org/doc/libs/1_35_0/libs/serialization/doc/wrappers.html
 */

namespace boost{ namespace serialization{
template<class Archive>
    inline void save(
            Archive & ar,
            const ::my_Matrix & t,
            const unsigned int file_version)
    {
        collection_size_type rows(t.rows());
        ar << BOOST_SERIALIZATION_NVP(rows);
        collection_size_type cols(t.cols());
        ar << BOOST_SERIALIZATION_NVP(cols);
        if (rows*cols) // save only if it is not empty
            ar << make_array(t.data(), t.size());
    } }}
Mark
Are you saying you want a version of the serialization method that handles higher order matrix types?
jon hanson
BTW you should update your question rather than add an answer (which isn't an answer).
jon hanson
Will do.Yeah - Looking to only write the save method once for many dimension blitz arrays, by using some type of typedef.
Mark