template help

K

Kamran

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
 
Z

Zara

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) */
 
E

Eric Pruneau

Kamran said:
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
 
G

Gianni Mariani

Kamran said:
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.
 

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