473,386 Members | 1,736 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,386 software developers and data experts.

template help



Hi

I am quite new in this so please bear with me.

I try to read some arrays of various types. I have
defined a function:

template <class T>
T* getArray(int size, int el_size)

which can be called like:

intehead_el = getArray<int>(no_in_el, sizeof(int));

or

intehead_el = getArray<double>(no_in_el, sizeof(double));
depending on the type of the array elements.

But the array element type itself is read from some
other variable in the input file.

My question is, is there a way which I could replace
the <class T> (int or double) with a generic name
when calling the function, depending on the type of
array element. Say that I just read the
type of the elements to be 'int' and this is stored
in a character array called 'type'. So then I would say:

intehead_el = getArray<type>(no_in_el, sizeof(type));
Thanks in advance

Nov 24 '05 #1
3 1233
On Thu, 24 Nov 2005 13:36:15 +0100, Kamran <ka****@uio.no> wrote:


Hi

I am quite new in this so please bear with me.

I try to read some arrays of various types. I have
defined a function:

template <class T>
T* getArray(int size, int el_size)

which can be called like:

intehead_el = getArray<int>(no_in_el, sizeof(int));

or

intehead_el = getArray<double>(no_in_el, sizeof(double));
depending on the type of the array elements.

But the array element type itself is read from some
other variable in the input file.

My question is, is there a way which I could replace
the <class T> (int or double) with a generic name
when calling the function, depending on the type of
array element. Say that I just read the
type of the elements to be 'int' and this is stored
in a character array called 'type'. So then I would say:

intehead_el = getArray<type>(no_in_el, sizeof(type));
Thanks in advance

Templates are instantiates at compile-time, not at run-time so the
strict answer is No.

BTW; whata about modifying your template, beacuse you already know the
size of the type if you know the type:

template <class T> T* getArray(size_t size)
/* el_type is exactly sizeof(T) */

Nov 24 '05 #2

"Kamran" <ka****@uio.no> a écrit dans le message de news:
dm**********@dolly.uninett.no...


Hi

I am quite new in this so please bear with me.

I try to read some arrays of various types. I have
defined a function:

template <class T>
T* getArray(int size, int el_size)

which can be called like:

intehead_el = getArray<int>(no_in_el, sizeof(int));

or

intehead_el = getArray<double>(no_in_el, sizeof(double));
Ok if T is the element type, why do you pass the size of the type to the
function?
you could just have

template<typename T>
T* getArray(int size)
{
int el_size = sizeof(T)
...
}
depending on the type of the array elements.

But the array element type itself is read from some
other variable in the input file.

My question is, is there a way which I could replace
the <class T> (int or double) with a generic name
when calling the function, depending on the type of
array element. Say that I just read the
type of the elements to be 'int' and this is stored
in a character array called 'type'. So then I would say:

intehead_el = getArray<type>(no_in_el, sizeof(type));
Thanks in advance


I'm not totally sure if I understand the question but the type
is either int or double? and you know this from a file...

you can do
// I assume you have the following enum:
enum TypeCode { eInt, eDouble }

// read from file and initialize t with either eInt or eDouble then do

if( t == eInt)
intehead_el = getArray<int>(no_in_el);
else if(t == eDouble)
intehead_el = getArray<double>(no_in_el);
else
... // not supported...

I'm not saying this is an award winnig desing but at least I think it do
what you want...

Eric
Nov 24 '05 #3
Kamran wrote:


Hi

I am quite new in this so please bear with me.

I try to read some arrays of various types. I have
defined a function:

template <class T>
T* getArray(int size, int el_size)
template <class T>
inline T* getArray(int count)
{
return getArray<T>(count, sizeof(T));
}

If you added this function to your header, you would not need the
sizeof() in your code.

which can be called like:

intehead_el = getArray<int>(no_in_el, sizeof(int));

or

intehead_el = getArray<double>(no_in_el, sizeof(double));
depending on the type of the array elements.

But the array element type itself is read from some
other variable in the input file.

My question is, is there a way which I could replace
the <class T> (int or double) with a generic name
when calling the function, depending on the type of
array element.
You can specialize based on type.

e.g.

template <typename T> struct Locator;

template <> struct Locator<double>
{
static char * Location();
};

template <> struct Locator<int>
{
static char * Location();
};

template <class T>
inline T* getArray(int count)
{
return getArray<T>(count, sizeof(T), Locator<T>::Location());
}
Say that I just read the type of the elements to be 'int' and this is stored
in a character array called 'type'. So then I would say:

intehead_el = getArray<type>(no_in_el, sizeof(type));


I'm not sure what you're trying to design, however it seems a little
strange.
Nov 24 '05 #4

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

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Rex_chaos | last post by:
Hi all, As some book tells, I try the following example of expression template. template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
3
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
7
by: shuisheng | last post by:
Dear All; Would you please help me to look at the following case: //! Rotation. enum Rotation { NON_CYCLIC, CYCLIC }; //! Rotation.
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.