Most important things about C++ templates… lesson learned

前端 未结 12 1451
暗喜
暗喜 2021-01-30 18:52

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

相关标签:
12条回答
  • 2021-01-30 19:25

    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.
    0 讨论(0)
  • 2021-01-30 19:28

    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.

    0 讨论(0)
  • 2021-01-30 19:28

    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.

    0 讨论(0)
  • 2021-01-30 19:31

    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.

    0 讨论(0)
  • 2021-01-30 19:38

    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...

    0 讨论(0)
  • 2021-01-30 19:41

    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,

    0 讨论(0)
提交回复
热议问题