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

Template data type

P: n/a
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
Share this Question
Share on Google+
9 Replies


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

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

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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.