[Faculty of Science Information and Computing Sciences] Concepts of programming languages C++ Template Metaprogramming Michiel Magendans Robin Ganpat Jelmer van Nuss Hugo Heemskerk Thomas Dirkse 1
[Faculty of Science Information and Computing Sciences] Metaprogramming Why program metaprograms? ◮ Programs writing programs, we’ve seen this before ◮ An introduction, not limited to C++ ◮ Very useful in the real world! ◮ Can go horribly wrong.. 2
[Faculty of Science Information and Computing Sciences] Hypothetical company ◮ We make administration software! Exciting! ◮ Web application that allows us to interact with a database ◮ Some business logic when interacting with the database 3
[Faculty of Science Information and Computing Sciences] What do we want to make? ◮ CRUD (Create, Read, Update, Delete) software requires insert, update, etc. functions/statements ◮ A database table is essentially a small set of named parameters, which themselves can have parameters ◮ Boilerplate heaven! So much code to write! ◮ Functions, getters, setters, loving it ◮ Always write the general case, don’t abstract 4
[Faculty of Science Information and Computing Sciences] How to monetize this ◮ Charge customer by programmer hour ◮ Hire lots more programmers ◮ Outsource ◮ Profjt! 5
[Faculty of Science Information and Computing Sciences] Lots of work though ◮ Work is getting cumbersome, let’s try metaprogramming Figure 1: 6
[Faculty of Science Information and Computing Sciences] Let’s reduce our workload ◮ Idea: Write a code generator! It writes the code for us! ◮ Set of 12 database table parameters to create 2k LoC, spread out over 16 fjles ◮ Stringly typed, some extra xml, no documentation necessary ◮ Manually add every generated fjle to a complex fjle tree 7
[Faculty of Science Information and Computing Sciences] More profjt! ◮ We have now guaranteed ourselves work for years to come! ◮ When the generator changes, we will have to retroactively update all previously generated fjles ◮ A job that would normally cost 30 minutes will now cost 10 hours at the minimum: we will make so much money! ◮ Technical debt? Owed by our customers, to us? Brilliant! 8
[Faculty of Science Information and Computing Sciences] Derp Figure 2: 9
[Faculty of Science Information and Computing Sciences] But.. why? ◮ This is not due to incompetence! ◮ It’s a set of common business problems coming together in an unfortunate way ◮ Strategy problems: no short-term fjnancial incentives to work effjciently, historical artifacts ◮ People problems: Unwillingness to change, attachment to own code, egos ◮ People problems are usually caused by strategy problems 10
[Faculty of Science Information and Computing Sciences] What should happen in the business ◮ Difgerent business model can make a huge difgerence! ◮ Upfront single-price purchase ◮ SaaS (Software as a Service, subscription fee) ◮ Difgerent languages can prevent difgerent types of situations from happening in the fjrst place 11
[Faculty of Science Information and Computing Sciences] What should happen in the tech ◮ DRY: Don’t Repeat Yourself ◮ Make the generic case trivial ◮ Extensibility: allow boilerplate to be written ifg necessary, and let it override very specifjc parts of the meta program ◮ Never let programmers touch the generated code - it might as well not exist ◮ Allow business logic to interact with metaprogrammed classes as if they were written manually ◮ IDE/text editor support, humans are not good at spotting syntax errors 12
[Faculty of Science Information and Computing Sciences] In OOP? ◮ You can do this in pure OOP too! ◮ However: requires multifaceted investment in framework ◮ Business logic becomes harder to separate from the framework ◮ Onboarding/training new developers becomes harder ◮ Language/IDEs/autocompletion might not be cooperative 13
[Faculty of Science Information and Computing Sciences] There are costs and obstacles ◮ If your chosen language does not support any types of metaprogramming, expect heavy costs, or rather don’t try at all ◮ Requires a higher upfront investment regardless ◮ Can be tougher to debug ◮ Small changes in the meta program afgect everything 14
[Faculty of Science Information and Computing Sciences] Takeaway ◮ Metaprogramming can be extremely useful and extremely damaging if done wrong ◮ Choice in language and programming practices greatly afgects how a business is, and can be run ◮ The opposite is also true! ◮ The question you need to ask yourself when choosing a language is: What mistakes will I allow the company’s programmers to make? What bugs are worth their time? 15
[Faculty of Science Information and Computing Sciences] C++ Templates What are they? ◮ A feature of C++ which allows functions and classes to operate with generic types. ◮ Generics are generic until the types are substituted for them at runtime. Templates are specialized at compile time. ◮ Templates allow explicit specialization. ◮ Type substition is done during compilation, not run-time. ◮ There are Function Templates and Class Templates 16
[Faculty of Science Information and Computing Sciences] int Max(int a, int b) { return a > b ? a : b; } cout << Max(23, 40); #> 40 Function Templates - A Simple example A possible Max()-function defjned in C++: Result: 17
[Faculty of Science Information and Computing Sciences] double Max(double a, double b) { return a > b ? a : b; } cout << Max(12.5, 48.21); #> 48.21 A Simple example The same function as previous slide, but for the double data type. Result: 18
[Faculty of Science Information and Computing Sciences] So what’s the difgerence? It is exactly the same logic, but the data type is difgerence. This is where Templates come in handy! 19
[Faculty of Science Information and Computing Sciences] template < typename T> T GetMax(T a, T b) { return a > b ? a : b; } cout << Max(23, 40); #> 40 The Max()-function using Function Templates Result: This invokes the template function with T == int. The function returns a value of int type. 20
[Faculty of Science Information and Computing Sciences] Class templates ◮ Function templates allow writing generic functions that will work on many types. ◮ The same idea applies for class templates. 21
[Faculty of Science Information and Computing Sciences] template < typename T> class ValueComparer { T first, second; public : ValueComparer(T a, T b) { first = a; second = b; } T getBiggerOne(); }; A Simple example (1) 22
[Faculty of Science Information and Computing #> 69 cout << vc_double.getBiggerOne(); cout << vc_int.getBiggerOne() << endl; ValueComparer <double> vc_double(29.23, 49.133); ValueComparer <int> vc_int(69, 34); } return first > second ? first : second; T ValueComparer<T>::getBiggerOne() { template < typename T> Sciences] #> 49.133 A Simple example (2) getBiggerOne() is a member function of the class template. Here is the implementation: Result: 23
[Faculty of Science Information and Computing Sciences] Template Specialization In some cases you may want a specifjc implementation for a specifjc data type. This is where Template Specialization comes in. 24
[Faculty of Science Information and Computing Sciences] template < typename T> class ValueComparer { T first, second; public : ValueComparer(T a, T b) { first = a; second = b; } T getBiggerOne(); } Template Specialization ValueComparer Template defjnition from previous slide: 25
[Faculty of Science return first > second ? first : second; ? first : second; return first.length() > second.length() string ValueComparer<string>::getBiggerOne() { template <> } T ValueComparer<T>::getBiggerOne() { Information and Computing template < typename T> Sciences] } Lets say we want to create a specifjc implementation for the data type: string. The function getBiggerOne() should return the string with the biggest length. cout << "We can do even more things here." << endl; 26
[Faculty of Science Information and Computing Sciences] ValueComparer <int> vc_int(69, 34); ValueComparer <double> vc_double(29.23, 49.133) ValueComparer <string> vc_string("aaa", "bb"); std::cout << vc_int.getBiggerOne() << endl; std::cout << vc_double.getBiggerOne(); std::cout << vc_string.getBiggerOne(); #> 69 #> 49.133 #> We can do even more things here. #> aaa The results 27
[Faculty of Science Information and Computing Sciences] Generic Programming ◮ Polymorphism ◮ Widely used concept in OO languages ◮ Used to associate difgerent specifjc behaviours with a single generic notation 28
[Faculty of Science Information and Computing Sciences] ◮ In C++, you make use of inheritance and virtual functions Figure 3: C++ Polymorphism 29
[Faculty of Science }; virtual Coordinate center_of_gravity() const; virtual void draw() const; // Both methods implementations in .cpp file. public : { class Circle : public GeoObj virtual Coordinate center_of_gravity() const = 0; Information and Computing virtual void draw() const = 0; public : { class GeoObj Sciences] } 30
Recommend
More recommend