C++ Guide for EOS Development - Templates

in #eos6 years ago

Featured Image

This post is part of my C++ Guide for EOS developers

  1. Basics
  2. Call by value / reference & Pointers
  3. Classes and Structs
  4. Templates
  5. Iterators
  6. Lambda Expressions
  7. Multi-index
  8. Header files

Templates

A language having static types comes with a lot of benefits because errors can be caught at compile time by type-checking.
However, it also introduces overhead when writing functions or classes, as they need to be written for a certain type.
What if you're writing a library and don't exactly know how your library is going to be used?
If you'd like to support more types, you have to repeat yourself and overload the function.

int max(int a, int b) {
    return a > b ? a : b;
}
max(5, 3); // works
max(5.0, 3.0) // does not work as these are _double_s and not _int_s.

You need to define another function for doubles:

double max(double a, double b) {
    return a > b ? a : b;
}

As you can see the function body is exactly the same in both cases. All that matters is that the type implements the comparison operator >.

For these use-cases, C++ provides type templates, generic types that you can use instead of specific ones.
This allows you to create functions or classes whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

// @url: https://repl.it/@MrToph/CPPBasics-Templates
#include <iostream>

// create a "function-template" with template type T
// T can now be used as any other type like int
template<class T>
T max(T a, T b)
{
  return a > b ? a : b;
}

// create a "class-template"
// class members can now be of the template type T
template <class T>
class pair {
    T values[2];
  public:
    pair(T first, T second)
    {
      values[0]=first;
      values[1]=second;
    }

    T first() const;

    T second() const;
};

// must use template<class T> syntax here again
template <class T>
T pair<T>::first() const {
  return values[0];
}

template <class T>
T pair<T>::second() const {
  return values[1];
}

int main()
{
    int iMax = max(3, 5);
    double dMax = max(3.0, 5.0);
    // class template instantiations are done
    // by passing the type in angle brackets
    pair<int> p(3, 5);
    std::cout << max(p.first(), p.second());
}

What happens behind the scenes is the same thing we did before by hand.
Being statically-typed, the code is analyzed and the types to any call to the template function can be resolved.
The compiler then instantiates a function for each specific type used.

Learn EOS Development Signup


Originally published at https://cmichel.io

Sort:  

nice work!
Followed + voted.

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!