473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2224
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 VariableSizeArr ay
{
char array_[ n ];
};

void Foo( int n )
{
VariableSizeArr ay<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 VariableSizeArr ay
{
char array_[ n ];
};

void Foo( int n )
{
VariableSizeArr ay<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
10283
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 possible, so I need "something like" a virtual function template. I read in some threads that there is a workaround using the visitor pattern. I read therefore in the book "Modern c++ Design" about this pattern, but couldnt find any help to my...
1
3277
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 and the result. Last 30 lines of the xslt may be the most interesting ones. I'm processing it with XMLSPY. Regardless of the xslt processor (ALTOVA / MSXML4 ) I get the same result.
9
2317
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 same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
1
2358
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 single class. Instead I have to separate implementation for binary and ascii in two different classes as I don't know how to use the traits technique for constructors. Any pointers very welcome !
7
2973
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{ Contents cn; list < BTree_node < Contents children; ........
5
2268
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 to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
5
2380
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 and trying to get the same code to work. Under Visual C++ 2005 I am seeing linker errors. The problem is in the definition of a couple of friend operators that the linker does not like for some reason. I am assuming I have a mistake in my...
6
391
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
5772
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8944
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, 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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.