tags:

views:

1450

answers:

11

What are most important things you know about templates: hidden features, common mistakes, best and most useful practices, tips...common mistakes/oversight/assumptions

I am starting to implement most of my library/API using templates and would like to collect most common patterns, tips, etc., found in practice.

Let me formalize the question: What is the most important thing you've learned about templates?

Please try to provide examples -- it would be easier to understand, as opposed to convoluted and overly dry descriptions

Thanks

A: 

It's important to understand separate compilation and the possibility of resulting executable size increases. If you instantiate the template with the same type in several C++ files, you will get the type reproduced multiple times, at least on some compilers.

Uri
Which compilers would those be?
anon
Hmm, I have certainly instantiated a template more than in one translation unit, without causing any "blowouts", as template instantiation doesn't violate one definition rule. However, if a compiler has a poor support of templates, anything is possible, even a WWIII!
Modern linkers discard the duplicates. This has been so for years - it's not really a compiler issue.
anon
I've seen quite a few legacy projects that use ancient compilers where this was an issue. Do most new compilers prelink to avoid this? I haven't done much C++ for quite a while, and executable size is not that much of an issue anymore.
Uri
+4  A: 

This question is a bit like "I'm going to implement most of my library using functions, what are the common mistakes in using functions?" It's hard to come up with sensible answers to such questions, but here's my advice - read a good book. I recommend "C++ Templates" by Vandevoorde & Josuttis,

anon
I've read parts of this book -- it's good. However, I just want to collect common tips encountered by a program. Thanks...
@Neil: Common mistakes in using functions include: passing parameters by value or by pointer/reference when you want the other way; returning pointers/references to local variables or temporaries; relying on the order of evaluation of arguments (which C++ does not guarantee).
j_random_hacker
+2  A: 

The STL is your friend.

Glenn
Not always, but I still don't know why the downvote.
Eduardo León
+1  A: 

Here are some rules:

  1. Don't write any templates unless you're writing a very, very generic library (STL and Boost are two prominent examples).
  2. Don't instantiate any non-trivial template too many times. Instantiating huge template classes is especially overkill. You should consider using inheritance and polymorphism (the simple way, I mean, using virtual functions).
  3. If you're writing any templates, knowing when to use const, mutable and volatile will save users of the template both compile and execution time.
  4. If you're instantiating any templates, use a good compiler.
Eduardo León
+4  A: 

I'd have to say Coplien's Curiously Recurring Template Pattern (CRTP) is the one template trick that I find myself reaching for over & over again. Essentially it allows you to inject statically customized functionality into a derived class by inheriting from a base class that is parameterized on the derived class name. Mind boggling, but amazingly useful (some call it static polymorphism).

Also, I'll second Neil Butterworth's advice to read "C++ Templates" and throw in Alexandrescu's Modern C++ Design.

Drew Hall
+3  A: 

Read Meyers's Effective STL and C++ books, and Alexandrescu's Modern C++ Design.

Meyers will give you the basics on easy mistakes to make and how to avoid them. Alexandrescu introduces you to a template-based programming model that should have you asking "Is this really a good idea?" the entire book.

Dan Olson
I read both of these books... collecting tips from people
+15  A: 

From "Exceptional C++ style", Item 7: function overload resolution happens before templates specialization. Do not mix overloaded function and specializations of template functions, or you are in for a nasty surprise at which function actually gets called.

template<class T> void f(T t) { ... }   // (a)
template<class T> void f(T *t) { ... }  // (b)
template<> void f<int*>(int *t) { ... } // (c)
...
int *pi; f(pi); // (b) is called, not (c)!

On top of Item 7:

Worse yet, if you omit the type in template specialization, a different function template might get specialized depending on the order of definition and as a result a specialized function may or may not be called.

Case 1:

template<class T> void f(T t) { ... }  // (a)
template<class T> void f(T *t) { ... } // (b)
template<> void f(int *t) { ... }      // (c) - specializes (b)
...
int *pi; f(pi); // (c) is called

Case 2:

template<class T> void f(T t) { ... }  // (a)
template<> void f(int *t) { ... }      // (c) - specializes (a)
template<class T> void f(T *t) { ... } // (b)
...
int *pi; f(pi); // (b) is called
Alex B
Could you give an example of this?
Hosam Aly
Yes. I've been kicked by this one too.
Tomek Szpakowicz
Nice example! +1. :)
Hosam Aly
+2  A: 

I tend to use templates quite a lot to avoid duplication of code, and to increase safety through compile checks.

In general, it helps to think while typing about what the compiler is going to do, and how the code will be generated for each type.

Being very iterative in development and building the template complexity little by little has helped me avoiding sinking in compile error messages. Don't forget to keep a simple (or mock) instantiation of the template somewhere, otherwise you might have some nasty surprises when you instantiate a monster template for the first time.

Finally, when there is no way out, get to read these compile error messages! They might look quite scary at first, but they really are helpful. Maybe at first extracting the first one, copying it in a text editor and making it look pretty will help getting used to them, and it quickly becomes second nature to read through.

small_duck
To avoid repetition of SOURCE code, you mean? Because with templates you have quite a lot of repeated MACHINE code.
Eduardo León
+7  A: 

This may not be popular, but I think it needs to be said.

Templates are complicated.

They are awesomely powerful, but use them wisely. Don't go too crazy, don't have too many template arguments... Don't have too many specializations... Remember, other programmers have to read this too.

And most of all, stay away from template metaprogramming...

dicroce
But template metaprogramming was supposed to be the fun part of it all!
Eduardo León
"And most of all, stay away from template metaprogramming..."Got point :-)
kyku
+2  A: 

Template Tip of the Day: Did you know you can specialize chosen functions of template instantiations:

#include <iostream>
#include <vector>

namespace std {
    template<>
    void vector<int>::clear() {
    std::cout << "Clearing..." << std::endl;
    resize(0);
    }
}

int main() {
    std::vector<int> v;
    v.push_back(1);
    v.clear();
}

ouputs: Clearing...

kyku
I knew that... a good one
+4  A: 

One common mistake is that a template constructor or assignment operator will not suppress the compiler generated one:

template <typename T>
class A {
public:
  template <typename S>
  A(A<S> const &);

  template <typename S>
  A & operator=(A<S> const &);

private:
  int * i;
};

Although these functions look like the copy constructor and copy assignment operator, the compiler does not see it that way and generates the implicit versions anyway. The result is that any actions performed by these functions (eg. deep copy of a member) will not take place when the object is copied or assigned to from the same type:

void foo (A<int>);

void bar () {
  A<int> a1;
  foo (a1);   // Implicitly generated copy ctor called

  A<long> a2;
  foo (a2);   // Template ctor called.

  A<int> a3;
  a3 = a1;   // Implicitly generated copy assignment operator called

  a3 = a2;   // Template assignment operator called
}

The reason for this behaviour is due to a special rule in overload resolution (13.3.3):

Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

[...]

— F1 is a non-template function and F2 is a function template specialization, or, if not that,

In the examples above, overload resolution sees two functions with the same signature, one of which is a template. The non template function (the implicitly generated copy constructor/copy assignment operator) wins and so is called.

Richard Corden
Another way to see it is that copy ctor and operator= are templates inside templates. When A<int> is instantiated, copy ctor and operator= member templates still do not exist as real functions, hence implicit copy ctor and operator= are generated for A<int>.
Alex B
To avoid this for assignment operator, you can have a non-template member that delegates to a template member: A } Can't see any way to do this for copy ctor, except for writing a separate non-template member.
Alex B