programming practice - coding utility functions in C++

Apr 9, 2010 at 11:05am
Hi,
I have a couple of questions:

1) How would you create a header file with full of function templates with declaration and definition separately... Is it possible to write the declarations in the .h file and definitions in the .cpp file (as we would do if not using templates?

2) Suppose I have implemented a basic vector class (including all operator overloading). Now, if I have to implement certain "utility functions".. for example, find the unique elements of a vector (created using this class of course), how I go about is creating a function template (in a separate header, say widgets.h) as,

1
2
3
4
5
6
7
8
9
10
#include "vector_class.h"

template <class T>
vec<T> unique(vec<T> &v) {

    vec<T> unique_v;
    // code to obtain the unique elements

    return unique_v;
}

As you can see, this code definitely requires creating a vector and returning it. This might be disadvantageous if the size of the vector is huge. I see 2 other possibilities.
a) in some cases, it might be possible (or make sense) to frame the algorithm in a way to store the result in the calling object itself (for ex: sort). This solves both the issues.
b) Implement it as a member function and all v.unique();, return using the *this pointer. This will also change the contents of the calling object.

Are they both equally good? (unless there is a third better option). Or do you think returning a vector isn't as bad as it sounds..?

PS: I have been advised that creating a pointer object (in the function template described above) and returning the address is also possible, but is not generally recommended as the user will have to take the task of freeing the pointer ?? I don't quite get a hold of it as to why. Could you show the code and tell how/why its so.

Thank you very much!
Apr 9, 2010 at 11:28am
Have a look at this, it describes preinstantiated template classes.
http://www.cplusplus.com/forum/unices/7252/#msg33687
Apr 9, 2010 at 3:31pm
Hi kbw,

I am sorry, but if you intended the link to help with my 1st question, then my question is about declaration and definition of function templates, not class templates. I am already using them. I decided to program certain utility functions to operate on the objects from the class I generated, using function templates.

The link was however useful in that I did not know we could restrict the data types of class templates using #ifndef #endif . (While we are at it, could this #ifndef .. #endif be also applied to certain member functions or even function templates??)

What I require with my 2nd question is an efficient (or normally programmed) way of creating custom utility functions to operate on the class making it more powerful. For example, if you have a matrix class, would you rather go about defining the task of find the row and column index of certain element in the matrix as a member function or as a function separately (outside the class by operating on the object)? The same goes to for example, calculating the inverse of a matrix. How do you decide it?

thank you
Apr 12, 2010 at 12:07pm
1. You can preinstantiate template functions in almost the same way you would template classes. As it happens, I needed to do it today on an old Microsoft C++ compiler, and even that was happy.

2. You need to think of efficiency; i.e. 2b. An example is, although STL provides containers and methods (like sort and find), they're not all called in the same way. The tree based containers implement their own sort and find as it can be done more efficiently. And users of map and vector simply accept that you call find or sort in different ways.
Last edited on Apr 12, 2010 at 2:19pm
Topic archived. No new replies allowed.