Template confusion

M

ma740988

In an attempt to write generic code for data types int, float,
double..., a template seems like the most viable solution.

So now, the .h file contains the prototypes for the functions and the
cpp contains the definitions of the functions. The compiler should
give errors - rightly so - if template<class T> is not specified
before the prototype AND before the header of the definition in the
..cpp.

template<class T>
T DoASpecialThing (T Number);

Then it dawned on me. Perhaps I could make the above definition look
'nicer'. As a result one thought involved creating a typedef for the
template defintions. Akin to (WARNING. I KNOW THIS DOESN'T WORK)

typedef template <class T>

In the end, I'm opting to create/would like to create my own sort of
"data type" that accepts unknowns, like template. Organized code is
one of the goals so I suspect a typedef would be option 1 or ..
Example(s) appreaciated.


It appears to me that I'm might be threading down the path of 'traits'
or specilization. Both of which I'm not savy on, nonetheless ...
 
B

Bob Hairgrove

In an attempt to write generic code for data types int, float,
double..., a template seems like the most viable solution.

So now, the .h file contains the prototypes for the functions and the
cpp contains the definitions of the functions. The compiler should
give errors - rightly so - if template<class T> is not specified
before the prototype AND before the header of the definition in the
.cpp.

You have to have the template definitions in the header as well, or
put them in a separate file included at the very end by the header. It
won't work to put them in a .cpp file (unless you might be using the
new "export" keyword, which I haven't used yet).
template<class T>
T DoASpecialThing (T Number);

Then it dawned on me. Perhaps I could make the above definition look
'nicer'. As a result one thought involved creating a typedef for the
template defintions. Akin to (WARNING. I KNOW THIS DOESN'T WORK)

typedef template <class T>

In the end, I'm opting to create/would like to create my own sort of
"data type" that accepts unknowns, like template. Organized code is
one of the goals so I suspect a typedef would be option 1 or ..
Example(s) appreaciated.


It appears to me that I'm might be threading down the path of 'traits'
or specilization. Both of which I'm not savy on, nonetheless ...

Typedef template is a feature which has often been requested, but has
not yet made it into the language. If you google for the concept, I'm
sure you will find many posts on comp.std.c++ dealing with the
subject.

Also, check out the "proxy" design pattern.
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top