473,407 Members | 2,546 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,407 software developers and data experts.

Template data type

jc
Hi all,

I have a data type to use that I can't modify its codes.

E.g. This template data type is data_type. When I declare a variable of
this data_type, I write as following:

data_type(8, 2) x; // Declare a data_type variable

Someone can confirm me that arguments in template data types must be
defined at compile time, like (8,2 in this example)?

Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments

In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2

Please tell me how can I do if I want to pass variables without
defining them.

Thanks

Jun 13 '06 #1
9 2208
jc wrote:
Hi all,

I have a data type to use that I can't modify its codes.

E.g. This template data type is data_type. When I declare a variable of
this data_type, I write as following:

data_type(8, 2) x; // Declare a data_type variable
I don't see how this could work. Did you mean:

data_type x(8,2);
Someone can confirm me that arguments in template data types must be
defined at compile time, like (8,2 in this example)?
Oh, those are template arguments? So you actually meant:

data_type<8, 2> x;

In that case: yes, the template arguments must be compile-time constants.
Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments

In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2
Well, instead of macros, you can also use a constant:

const int M = 8;
Please tell me how can I do if I want to pass variables without
defining them.


What do you mean by "without defining them"? If there is no variable, there
is nothing to pass, so you must define them.

Jun 13 '06 #2
jc wrote:
I have a data type to use that I can't modify its codes.

E.g. This template data type is data_type. When I declare a variable
of this data_type, I write as following:

data_type(8, 2) x; // Declare a data_type variable
Methinks that you probably write

data_type<8, 2> x;
Someone can confirm me that arguments in template data types must be
defined at compile time, like (8,2 in this example)?
Yes, they must.
Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments
Not possible. Unless 'M' and 'N' can be evaluated at compile-time
and yield constant expressions, you can't use them.
In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2

Please tell me how can I do if I want to pass variables without
defining them.


If you want them to be true run-time variables, you need to define
the constructor to take arguments and the write

data_type y(M, N);

However, if 'data_type' is a template, and you can't change the code,
you're pretty much SOL. Templates have to have their arguments
defined at *compile-time* as constants.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '06 #3
jc wrote:
Hi all,

I have a data type to use that I can't modify its codes.

E.g. This template data type is data_type. When I declare a variable of
this data_type, I write as following:

data_type(8, 2) x; // Declare a data_type variable

Someone can confirm me that arguments in template data types must be
defined at compile time, like (8,2 in this example)?

Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments

In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2

Please tell me how can I do if I want to pass variables without
defining them.

Thanks


First of all, you need to use angle brackets for your templates, not
parentheses:

data_type<8,2> x;

If your code does use parentheses, then a macro is being employed, but
I don't know why it would be that way.

As for variables as template arguments, you can't do it. Class
templates are not real classes until you supply template arguments at
compile-time, at which point the real class is instantiated (that is, a
new type is created from the template but with the missing details
filled in by the template parameters). C++'s type checking system won't
let you have variables as template arguments because it must have all
the details about every type at compile-time. It wouldn't know how to
create that type (e.g., how much memory it uses) until run-time if you
did something like this:

template< int n >
struct VariableSizeArray
{
char array_[ n ];
};

void Foo( int n )
{
VariableSizeArray<n> v; // Error!
// ...
}

Of course, you can pass variables to the class template's constructor,
which is probably what you should be doing anyway.

Cheers! --M

Jun 13 '06 #4
jc
Thank you very much Rolf for your answer.

Oh, those are template arguments? So you actually meant:

data_type<8, 2> x;

In that case: yes, the template arguments must be compile-time constants.
Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments

In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2
Well, instead of macros, you can also use a constant:

const int M = 8;


Yes, I can do this. But somehow I must have constants, not variables.
What I meant below is that I dont want macros or constant like you
suggested. I want to pass variables
Please tell me how can I do if I want to pass variables without
defining them.


What do you mean by "without defining them"? If there is no variable, there
is nothing to pass, so you must define them.


Jun 13 '06 #5
jc
Thank you very much Rolf for your answer.

Oh, those are template arguments? So you actually meant:

data_type<8, 2> x;

In that case: yes, the template arguments must be compile-time constants.
Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments

In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2
Well, instead of macros, you can also use a constant:

const int M = 8;


Yes, I can do this. But somehow I must have constants, not variables.
What I meant below is that I dont want macros or constant like you
suggested. I want to pass variables
Please tell me how can I do if I want to pass variables without
defining them.


What do you mean by "without defining them"? If there is no variable, there
is nothing to pass, so you must define them.


Jun 13 '06 #6
jc
Thank you mlimber for your answer.

mlimber wrote:
jc wrote:
Hi all,

I have a data type to use that I can't modify its codes.

E.g. This template data type is data_type. When I declare a variable of
this data_type, I write as following:

data_type(8, 2) x; // Declare a data_type variable

Someone can confirm me that arguments in template data types must be
defined at compile time, like (8,2 in this example)?

Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments

In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2

Please tell me how can I do if I want to pass variables without
defining them.

Thanks
First of all, you need to use angle brackets for your templates, not
parentheses:

data_type<8,2> x;

If your code does use parentheses, then a macro is being employed, but
I don't know why it would be that way.

Yes, some macro is being used, but I also don't know why because I dont
have access to modify this template data type.


As for variables as template arguments, you can't do it. Class
templates are not real classes until you supply template arguments at
compile-time, at which point the real class is instantiated (that is, a
new type is created from the template but with the missing details
filled in by the template parameters). C++'s type checking system won't
let you have variables as template arguments because it must have all
the details about every type at compile-time. It wouldn't know how to
create that type (e.g., how much memory it uses) until run-time if you
did something like this:

template< int n >
struct VariableSizeArray
{
char array_[ n ];
};

void Foo( int n )
{
VariableSizeArray<n> v; // Error!
// ...
}

Of course, you can pass variables to the class template's constructor,
which is probably what you should be doing anyway.

Cheers! --M


Jun 13 '06 #7
jc
Thank you very much Victor for your answer. It's helpful.

Victor Bazarov wrote:
jc wrote:
I have a data type to use that I can't modify its codes.

E.g. This template data type is data_type. When I declare a variable
of this data_type, I write as following:

data_type(8, 2) x; // Declare a data_type variable


Methinks that you probably write

data_type<8, 2> x;
Someone can confirm me that arguments in template data types must be
defined at compile time, like (8,2 in this example)?


Yes, they must.
Now I want to use variables for arguments, e.g:

data_type(M, N) y; // Use variables to define data_type arguments


Not possible. Unless 'M' and 'N' can be evaluated at compile-time
and yield constant expressions, you can't use them.
In the second example, It doesn't work when compiling. In order to
correct it, I can define constants M, N before:

#define M 8
#define N 2

Please tell me how can I do if I want to pass variables without
defining them.


If you want them to be true run-time variables, you need to define
the constructor to take arguments and the write

data_type y(M, N);

However, if 'data_type' is a template, and you can't change the code,
you're pretty much SOL. Templates have to have their arguments
defined at *compile-time* as constants.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jun 13 '06 #8
jc wrote:
First of all, you need to use angle brackets for your templates, not
parentheses:

data_type<8,2> x;

If your code does use parentheses, then a macro is being employed, but
I don't know why it would be that way.


Yes, some macro is being used, but I also don't know why because I dont
have access to modify this template data type.


You don't need to modify it to see what it is doing. The macro and
(chances are) the template class must be fully defined somewhere in
your header files (not in a library that is linked in).

Cheers! --M

Jun 13 '06 #9
jc
Thank you mlimber for your helpful answer.

Cheers,
J
mlimber wrote:
jc wrote:
First of all, you need to use angle brackets for your templates, not
parentheses:

data_type<8,2> x;

If your code does use parentheses, then a macro is being employed, but
I don't know why it would be that way.


Yes, some macro is being used, but I also don't know why because I dont
have access to modify this template data type.


You don't need to modify it to see what it is doing. The macro and
(chances are) the template class must be fully defined somewhere in
your header files (not in a library that is linked in).

Cheers! --M


Jun 13 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sebastian Faust | last post by:
Hi, I have a design problem about which I am thinking now for a while and still couldnt find any help in deja. What I need is something like a virtual function template. I know that this is not...
1
by: Rolf Kemper | last post by:
Dear Experts, I'm going to create an Excell spreadsheet xml. So far things work very well. But in case I add <Row> elements by a recursive template call it goes wrong. See the attached XSLT...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
5
by: 2beagles | last post by:
I have a template for a three dimensional array that I have been working on using Visual C++ 6.0. Under version 6 this code worked fine. However, last night I downloaded Visual C++ 2005 Express and...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
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
jinu1996
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...
0
tracyyun
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.