By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

template and variable parameters

P: n/a
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

Dec 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ma**********@wanadoo.fr wrote:
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-lit...html#faq-10.18) with a
CreateMatrix object that has SetHeight(), SetWidth(), and SetDepth()
members or a generic SetDim() member.

Cheers! --M

Dec 13 '05 #2

P: n/a
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) ???

Dec 22 '05 #3

P: n/a

chowy wrote:
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

Dec 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.