Download presentation
Presentation is loading. Please wait.
Published byMagdalene Morton Modified over 9 years ago
1
C++ Concepts : Introduction, Goals, and Applications Mohammad Soryani Mazandaran University of Science and Technology Soryani@ustmb.ac.ir
2
C++ Concepts: Introduction, Goals, and Applications 1 Preface C++ template system Generic programming Concepts goals and applications Concepts considerations Concepts structure Concept maps Concept refinement Concept based overloading Optimized algorithms History and current situation Outline
3
C++ Concepts: Introduction, Goals, and Applications 2 Preface -All of our discussion will be inside C++ -Which part of it? -Template system -What does it have to do with CBSE? -We’ll get to it soon
4
C++ Concepts: Introduction, Goals, and Applications 3 Templates -Let’s take a look at a normal function that gets two integers and returns the min : int getmin (int a, int b) { return (a<b ? a : b); }
5
C++ Concepts: Introduction, Goals, and Applications 4 Templates Template T getmin (T a, T b) { return (a<b ? a : b); } - There is a library of templates in C++ called STL(Standard Template Library)
6
C++ Concepts: Introduction, Goals, and Applications 5 Templates - How can we use this template? - Let’s see some examples : int x,y; getmin (x,y); double i,j; getmin (i,j); - What happens when we use it?
7
C++ Concepts: Introduction, Goals, and Applications 6 Templates Some questions about the current C++ template system : - Is a user allowed to use a template with any type? - Can we write their requirements in C++? - What would happen if a user uses a wrong type? - Who is responsible for the error? - Will we get informed about the cause of the error? - Is it possible to turn a wrong type into a right one?
8
C++ Concepts: Introduction, Goals, and Applications 7 Generic programming -A methodology for the development of reusable software libraries - Three primary tasks: - Categorize the abstractions in a domain into concepts - Implement generic algorithms based on the concepts - Build concrete models of the concepts
9
C++ Concepts: Introduction, Goals, and Applications 8 Generic programming - Lifting is a very important tool Lifting in a glance : 1- Study the concrete implementations of an algorithm 2- Lift away unnecessary requirements to produce a more abstract algorithm and bundle these requirements into concepts. 3- Repeat the lifting process until we have obtained a generic algorithm
10
C++ Concepts: Introduction, Goals, and Applications 9 Concepts Goals and Applications - Bringing the full power of Generic programming into C++ - Coding the requirements of data types directly directly into C++ - Better errors for template system - adoption
11
C++ Concepts: Introduction, Goals, and Applications 10 Considerations - Simplicity - Backward compatibility
12
C++ Concepts: Introduction, Goals, and Applications 11 Concepts Structure - Recall the getmin() function; let’s write the requirements of it’s type parameters. concept LessThanComparable { bool operator<(T x, T y); } - Now let’s make our template constrained template requires LessThanComparable T getmin(T a, T b) { return a < b ? a : b; }
13
C++ Concepts: Introduction, Goals, and Applications 12 Concept Maps - Consider we have a type called color for wich there is no “+” operator - We want to apply an add algorithm to two variables with type color - We make it possible by defining a map
14
C++ Concepts: Introduction, Goals, and Applications 13 Concept Maps - If some operations are not defined for a type we can define them in a concept map concept_map Addable { color operator+(color x, color y) { return x.mix(y); } }
15
C++ Concepts: Introduction, Goals, and Applications 14 Concept Refinement - It seems like inheritance - The refining concept inherits all of the base concept's requirements - For example we can refine Polygon concept to make EquilateralPolygon concept : concept EquilateralPolygon : Polygon { … } - A type that is an EquilateralPolygon can be used in any algorithm that expects a Polygon
16
C++ Concepts: Introduction, Goals, and Applications 15 Concept Based Overloading - We have different algorithms with an identical name - The compiler chooses which algorithm should be used - Choosing the algorithm is based on the concept(requirements) that used types meet.
17
C++ Concepts: Introduction, Goals, and Applications 16 Optimized algorithms - We have an algorithm that calculates the perimeter of a polygon by adding the side lengths of it (only the types that are Polygon can use this algorithm) - For equilateral polygons we can write a better algorithm that has just one multiplication (only the types that are EquilateralPolygon can use this algorithm)
18
C++ Concepts: Introduction, Goals, and Applications 17 Optimized algorithms - We make EquilateralPolygon concept by refining the Polygon concept (concept refinement) - We give both of the algorithms(functions) that do the same thing an identical name (overloaded versions)
19
C++ Concepts: Introduction, Goals, and Applications 18 Optimized algorithms - The compiler uses the better algorithm for types that meet the refined concept (Multiplication for EquilateralPolygon in this case) - If the used type just meets the base concept the normal algorithm is used
20
C++ Concepts: Introduction, Goals, and Applications 19 History and Current Situation - Bjarne Stroustrup says that he has worked on concepts for more than seven years [3] - Concepts is the work of many people, but the two universities that are doing much of the work are Texas A&M And Indiana university [2] - At the July 2009 meeting in Frankfurt, Germany, the C++ Standards Committee voted to remove concepts from C++0x[3] - Maybe a significantly improved version of "concepts" will be available in five years [3]
21
C++ Concepts: Introduction, Goals, and Applications 19 References [1] Douglas. Gregor, “Easier C++: An Introduction to Concepts”, http://www.devx.com/SpecialReports/Article/38864/1954http://www.devx.com/SpecialReports/Article/38864/1954, August 18, 2008. [2] Douglas. Gregor, “Concepts: Extending C++ Templates For Generic Programming”, Google TechTalks, February 21, 2007. [3] Bjarne. Stroustrup, “The C++0x Remove Concepts Decision”, Dr. Dobb’s Journal, http://www.ddj.com/cpp/218600111,http://www.ddj.com/cpp/218600111 Jul 22, 2009. [4] Douglas. Gregor, “ConceptsC++ Tutorial”, April 11, 2007.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.