template and variable parameters

M

martin.druon

Hi,

I created a template class to represent hypermatrix. I would like to
add methods where the number of parameters are checked during the
compilation time. For example :

template <size_t dim>
class Matrix
{
protected :
... // member datas

public:
...
Matrix( ????? ) { } // -> all of my parameters are "int"
...

void create ( ????? ) { } // -> all of my parameters are "int"
}

template <>
class Matrix<0>
{
protected :
... // member datas

public:
...
Matrix( ????? ) { }
...

void create ( ????? ) { }
}



in main.cpp :


int main(int argc, char **argv)
{
Matrix<3> m1(2, 5, 4); // OK
Matrix<3> m2(2, 5, 4, 8, 3); // compilation error

Matrix<3> m3; // OK
m3.create(4, 6, 5); // OK
m3.create(4); // compilation error
}

How I can do that ???

I try recursive method (with "operator ," overloading, ...) and with
lists but :
- either the number of parameters is not checked
- either the syntax is not like I want (without {...} or int[] = ...)

I try also to overload the "cast operator" but I never success... for
example to do this :

before : 1,2; // -> the ",2" is ignored (not "operator ," defined
with "int")
after : 1,2; // "1" is automaticaly cast to a new created class
(Lst for example) so the "," of ",2" is the "operator ," of this class
Lst

Do you understand ???

All of your ideas are welcome...

Thanks
 
M

mlimber

Hi,

I created a template class to represent hypermatrix. I would like to
add methods where the number of parameters are checked during the
compilation time. For example :

template <size_t dim>
class Matrix
{
protected :
... // member datas

public:
...
Matrix( ????? ) { } // -> all of my parameters are "int"
...

void create ( ????? ) { } // -> all of my parameters are "int"
}

template <>
class Matrix<0>
{
protected :
... // member datas

public:
...
Matrix( ????? ) { }
...

void create ( ????? ) { }
}



in main.cpp :


int main(int argc, char **argv)
{
Matrix<3> m1(2, 5, 4); // OK
Matrix<3> m2(2, 5, 4, 8, 3); // compilation error

Matrix<3> m3; // OK
m3.create(4, 6, 5); // OK
m3.create(4); // compilation error
}

How I can do that ???

I try recursive method (with "operator ," overloading, ...) and with
lists but :
- either the number of parameters is not checked
- either the syntax is not like I want (without {...} or int[] = ...)

I try also to overload the "cast operator" but I never success... for
example to do this :

before : 1,2; // -> the ",2" is ignored (not "operator ," defined
with "int")
after : 1,2; // "1" is automaticaly cast to a new created class
(Lst for example) so the "," of ",2" is the "operator ," of this class
Lst

Do you understand ???

All of your ideas are welcome...

Thanks

There are two solutions that I see:

1. Use partial specialization:

template<unsigned dims>
struct Matrix
{
Matrix( unsigned, unsigned );
Matrix( unsigned, unsigned, unsigned );
// ...
};

template<>
class Matrix<2>::Matrix(
unsigned height,
unsigned width )
{ /*...*/ }

template<>
class Matrix<3>::Matrix(
unsigned height,
unsigned width,
unsigned depth )
{ /*...*/ }

Note that you should not define Matrix(unsigned,unsigned,unsigned) for
Matrix<2> or Matrix(unsigned,unsigned) for Matrix<3>. Then any attempt
to link will fail if those functions are called.

2. You could use method chaining (cf.
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18) with a
CreateMatrix object that has SetHeight(), SetWidth(), and SetDepth()
members or a generic SetDim() member.

Cheers! --M
 
C

chowy

Thanks...

but with these methods, I can't use n-dimensional matrix...

for example, to use a 5-d matrix, I must define before :

Matrix( unsigned, unsigned, unsigned, unsigned, unsigned );

template<>
class Matrix<5>::Matrix(
unsigned dim1,
unsigned dim2,
unsigned dim3,
unsigned dim4,
unsigned dim5 )
{ /*...*/ }

and for a 8-d matrix... !!!!

Perhaps it's impossible to do what I want ???
Perhaps I must use a pre-processor before (to generate code) ???
 
D

deane_gavin

chowy said:
Thanks...

but with these methods, I can't use n-dimensional matrix...

for example, to use a 5-d matrix, I must define before :

Matrix( unsigned, unsigned, unsigned, unsigned, unsigned );

template<>
class Matrix<5>::Matrix(
unsigned dim1,
unsigned dim2,
unsigned dim3,
unsigned dim4,
unsigned dim5 )
{ /*...*/ }

and for a 8-d matrix... !!!!

Perhaps it's impossible to do what I want ???
Perhaps I must use a pre-processor before (to generate code) ???

If you do decide to go the preprocessor route, the boost preprocessor
library might be useful to you.

http://www.boost.org/libs/preprocessor/doc/index.html

I've not used it so I don't know whether it would do what you need. But
some of the examples in the documentation seem similar to your
question.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top