473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

template and variable parameters

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
3 1745
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone kindly explain why non-type template parameters are required by giving some examples where their uses are clearly favourable to other alternatives? I cannot think of any good use for...
4
by: Steven T. Hatton | last post by:
#include <iostream> namespace ns{ const char name = "This is a Class Name";//won't compile //char name = "This is a Class Name"; // compiles template <typename T, char* Name_CA=name> struct...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
11
by: Fan Yang | last post by:
I'm reading Modern C++ Design, and it is saying "Variable template parameters simply don't exist." But I find VC7.1 & VC8 support this feature.Who can tell me that which is right -_-b Many thanks.
6
by: Bartholomew Simpson | last post by:
I'm trying to avoid (or at least, minimize) duplicity of effort. I have the following function: void Permissions::Exists(const unsigned int id, std::vector<Permission>::const_iterator& iter) {...
0
by: Orin | last post by:
Hi, Here is my task: - need to get text from a template variable. For example we have a text with templates "Cars" and "Producers": "{{Cars|Toyota=blablabla |Moskvitch=blablabla...
2
by: Pierre Yves | last post by:
Hi there, Sorry for the double subject but I feel they are related. I'm not pretty sure there would be an answer but I reckon there must be a way to make it work. I would like to write the...
2
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.