Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template and variable parameters

Reply
Thread Tools

template and variable parameters

 
 
martin.druon@wanadoo.fr
Guest
Posts: n/a
 
      12-13-2005
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      12-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
chowy
Guest
Posts: n/a
 
      12-22-2005
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) ???

 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-22-2005

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template template arguments and default parameters avasilev C++ 2 10-06-2011 01:40 PM
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Using a SQL statement with variable parameters and variable criteria Froefel ASP .Net 1 07-04-2007 09:38 AM
Template specialization for templated and primitive type template parameters case2005 C++ 3 02-13-2005 06:53 PM
implementing policy classes with template template parameters ian C++ 5 10-09-2003 03:19 PM



Advertisments