Template data type

J

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
 
R

Rolf Magnus

jc said:
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.
 
V

Victor Bazarov

jc said:
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 said:
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
 
M

mlimber

jc said:
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
 
J

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.


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
 
J

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.


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
 
J

jc

Thank you mlimber for your answer.
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
 
M

mlimber

jc said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top