tags:

views:

195

answers:

8
template<class T>
void swap(T &a, T &b)
{
    T t;

    t = a; 
    a = b;
    b = t;
}

replace

void swap(int &a, int &b)
{
    int t;

    t = a;
    a = b;
    b = t;
}

This is the simplest example I could come up with,but there should be many other complicated functions.Should I make all methods I write templated if possible?

Any disadvantages to do this?

thanks.

+3  A: 

Make them as generic as you can trivially make them. If it's truly trivial (such as the above example) then it takes no extra work, and might save you some work in the future

Martin
It's rude to downvote a question without at least commenting why.
Martin
A: 

There are disadvantages to using templates all the time. It (can) greatly increase the compilation time of your program and can make compilation errors more difficult to understand.

As taldor said, don't make your functions more generic than they need to be.

Jeff Foster
+10  A: 

Genericity has the advantage of being reusable. However, write things generic, only if:

  1. It doesn't take much more time to do that, than do it non-generic
  2. It doesn't complicate the code more than a non-generic solution
  3. You know will benefit from it later

However, know your standard library. The case you presented is already in STL as std::swap.

Also, remember that when writing generically using templates, you can optimize special cases by using template specialization. However, always to it when it's needed for performance, not as you write it.

Also, note that you have the question of run-time and compile-time performance here. Template-based solutions increase compile-time. Inline solutions can but not must decrease run-time.

`Cause "Premature optimization and genericity is the root of all evil". And you can quote me on that -_-.

Kornel Kisielewicz
+9  A: 

Reusable code is reusable only if you actually reuse it. so write the function naturally in the first instance. If a bit later you come across a situation where the code could be reused with a little tweak, go back and refactor it, It is at the refactoring stage you should consider writing template functions.

anon
+4  A: 

The simplest answer to your question is what many people smarter than myself have been saying for years:

Never write more than the minimum you can get away with.

Swizec Teller
A: 

You may take a look at the function parameters and the way they are used. If all operations are done through overloaded operators the function may be very generic and a good candidate to become a template. Otherwise, the presence of very specialized class types and functions calls may make generic reusability very problematic and any eventual flexibility should be rather realized through polymorphism.

jszpilewski
+1  A: 

The first time you write swap you shouldn't

The second time it might be tempting but sometime you can get away without making the whole thing a mess

The third time it should be clear that you must. However depending on how many places you've used one and two it might be time consuming so the second time should be a good decision

Eric
A: 

A few thoughts:

  1. Know the STL. There is std::swap already. Instead of spending your time making everything as generic as possible, spend your time becoming more familiar with the STL.

  2. Don't do it till you need it: "Always implement things when you actually need them, never when you just foresee that you need them."---Ron Jeffries. If you don't actually reuse the code you didn't write reusable code, you wrote unnecessary code. Unnecessary code is expensive to develop, expensive to test, and expensive to maintain. Don't forget opportunity cost!

  3. Keep things simple: "Make everything as simple as possible, but not simpler."---Albert Einstein. This is KISS.

Jason