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 3 1765 ma**********@wa nadoo.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<unsign ed dims>
struct Matrix
{
Matrix( unsigned, unsigned );
Matrix( unsigned, unsigned, unsigned );
// ...
};
template<>
class Matrix<2>::Matr ix(
unsigned height,
unsigned width )
{ /*...*/ }
template<>
class Matrix<3>::Matr ix(
unsigned height,
unsigned width,
unsigned depth )
{ /*...*/ }
Note that you should not define Matrix(unsigned ,unsigned,unsig ned) 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
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>::Matr ix(
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) ???
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>::Matr ix( 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 them except to create different
sizes of static arrays from a template, but this could be done by
creating a dynamic array of different sizes...
|
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 A {
A()
: _data(15)
|
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 class B has an instance of
template class A, which has some base type T (usually int or double).
However, the base type T is important to calculations...
|
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)
{
T retVal = val;
if ( retVal < vmin )
retVal = vmin;
|
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.
| |
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)
{
if (m_permissions.empty())
iter = m_permissions.end() ;
std::vector<Permission>::const_iterator cit=m_permissions.begin();
|
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 |BMW=blabla ...}}
{{Producers|TableLTD=blablabla |PhoneINC=Developed in 1998 in Kiev,
|
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 following bit of code:
8<----------------------------
|
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>
class Ring {
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |