🗊 Презентация The Essence of C++ with examples in C++84, C++98, C++11, and C++14

Нажмите для полного просмотра!
The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №1 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №2 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №3 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №4 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №5 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №6 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №7 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №8 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №9 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №10 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №11 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №12 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №13 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №14 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №15 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №16 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №17 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №18 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №19 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №20 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №21 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №22 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №23 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №24 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №25 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №26 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №27 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №28 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №29 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №30 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №31 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №32 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №33 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №34 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №35 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №36 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №37 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №38 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №39 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №40 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №41 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №42 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №43 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №44 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №45 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №46 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №47 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №48 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №49 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №50 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №51 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №52 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №53 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №54 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №55 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №56 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №57 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №58 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №59 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №60 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №61 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №62 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №63 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №64 The Essence of C++ with examples in C++84, C++98, C++11, and C++14, слайд №65

Содержание

Вы можете ознакомиться и скачать презентацию на тему The Essence of C++ with examples in C++84, C++98, C++11, and C++14. Доклад-сообщение содержит 65 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1


The Essence of C++ with examples in C++84, C++98, C++11, and C++14 Bjarne Stroustrup Texas A&M University
Описание слайда:
The Essence of C++ with examples in C++84, C++98, C++11, and C++14 Bjarne Stroustrup Texas A&M University

Слайд 2


Abstract C++11 is being deployed and the shape of C++14 is becoming clear. This talk examines the foundations of C++. What is essential? What sets...
Описание слайда:
Abstract C++11 is being deployed and the shape of C++14 is becoming clear. This talk examines the foundations of C++. What is essential? What sets C++ apart from other languages? How does new and old features support (or distract from) design and programming relying on this essence. I focus on the abstraction mechanisms (as opposed to the mapping to the machine): Classes and templates. Fundamentally, if you understand vector, you understand C++. Type safety and resource safety are key design aims for a program. These aims must be met without limiting the range of applications and without imposing significant run-time or space overheads. I address issues of resource management (garbage collection is not an ideal answer and pointers should not be used as resource handles), generic programming (we must make it simpler and safer), compile-time computation (how and when?), and type safety (casts belongs in the lowest-level hardware interface). I will touch upon move semantics, exceptions, concepts, type aliases, and more. My aim is not so much to present novel features and technique, but to explore how C++’s feature set supports a new and more effective design and programming style. Primary audience Experienced programmers with weak C++ understanding Academics/Teachers/Mentors Architects (?)

Слайд 3


Overview Aims and constraints C++ in four slides Resource management OOP: Classes and Hierarchies (very briefly) GP: Templates Requirements checking...
Описание слайда:
Overview Aims and constraints C++ in four slides Resource management OOP: Classes and Hierarchies (very briefly) GP: Templates Requirements checking Challenges

Слайд 4


What did/do I want? Type safety Encapsulate necessary unsafe operations Resource safety It’s not all memory Performance For some parts of almost all...
Описание слайда:
What did/do I want? Type safety Encapsulate necessary unsafe operations Resource safety It’s not all memory Performance For some parts of almost all systems, it’s important Predictability For hard and soft real time Teachability Complexity of code should be proportional to the complexity of the task Readability People and machines (“analyzability”)

Слайд 5


Who did/do I want it for? Primary concerns Systems programming Embedded systems Resource constrained systems Large systems Experts “C++ is expert...
Описание слайда:
Who did/do I want it for? Primary concerns Systems programming Embedded systems Resource constrained systems Large systems Experts “C++ is expert friendly” Novices C++ Is not just expert friendly

Слайд 6


What is C++?
Описание слайда:
What is C++?

Слайд 7


C++
Описание слайда:
C++

Слайд 8


Programming Languages
Описание слайда:
Programming Languages

Слайд 9


What does C++ offer? Not perfection Of course Not everything for everybody Of course A solid fundamental model Yes, really 30+ years of real-world...
Описание слайда:
What does C++ offer? Not perfection Of course Not everything for everybody Of course A solid fundamental model Yes, really 30+ years of real-world “refinement” It works Performance A match for anything The best is buried in “compatibility stuff’’ long-term stability is a feature

Слайд 10


What does C++ offer? C++ in Four slides Map to hardware Classes Inheritance Parameterized types If you understand int and vector, you understand C++...
Описание слайда:
What does C++ offer? C++ in Four slides Map to hardware Classes Inheritance Parameterized types If you understand int and vector, you understand C++ The rest is “details” (1,300+ pages of details)

Слайд 11


Map to Hardware Primitive operations => instructions +, %, ->, [], (), … int, double, complex, Date, … vector, string, thread, Matrix, … Objects can...
Описание слайда:
Map to Hardware Primitive operations => instructions +, %, ->, [], (), … int, double, complex, Date, … vector, string, thread, Matrix, … Objects can be composed by simple concatenation: Arrays Classes/structs

Слайд 12


Classes: Construction/Destruction From the first week of “C with Classes” (1979) class X { // user-defined type public: // interface X(Something); //...
Описание слайда:
Classes: Construction/Destruction From the first week of “C with Classes” (1979) class X { // user-defined type public: // interface X(Something); // constructor from Something ~X(); // destructor // … private: // implementation // … }; “A constructor establishes the environment for the members to run in; the destructor reverses its actions.”

Слайд 13


Abstract Classes and Inheritance Insulate the user from the implementation struct Device { // abstract class virtual int put(const char*) = 0; //...
Описание слайда:
Abstract Classes and Inheritance Insulate the user from the implementation struct Device { // abstract class virtual int put(const char*) = 0; // pure virtual function virtual int get(const char*) = 0; }; No data members, all data in derived classes “not brittle” Manipulate through pointer or reference Typically allocated on the free store (“dynamic memory”) Typically requires some form of lifetime management (use resource handles) Is the root of a hierarchy of derived classes

Слайд 14


Parameterized Types and Classes Templates Essential: Support for generic programming Secondary: Support for compile-time computation template class...
Описание слайда:
Parameterized Types and Classes Templates Essential: Support for generic programming Secondary: Support for compile-time computation template class vector { /* … */ }; // a generic type vector constants = {3.14159265359, 2.54, 1, 6.62606957E-34, }; // a use template void sort (Cont& c) { /* … */ } // a generic function sort(constants); // a use

Слайд 15


Not C++ (fundamental) No crucial dependence on a garbage collector GC is a last and imperfect resort No guaranteed type safety Not for all constructs...
Описание слайда:
Not C++ (fundamental) No crucial dependence on a garbage collector GC is a last and imperfect resort No guaranteed type safety Not for all constructs C compatibility, history, pointers/arrays, unions, casts, … No virtual machine For many reasons, we often want to run on the real machine You can run on a virtual machine (or in a sandbox) if you want to

Слайд 16


Not C++ (market realities) No huge “standard” library No owner To produce “free” libraries to ensure market share No central authority To approve,...
Описание слайда:
Not C++ (market realities) No huge “standard” library No owner To produce “free” libraries to ensure market share No central authority To approve, reject, and help integration of libraries No standard Graphics/GUI Competing frameworks XML support Web support …

Слайд 17


Resource Management
Описание слайда:
Resource Management

Слайд 18


Resource management A resource should be owned by a “handle” A “handle” should present a well-defined and useful abstraction E.g. a vector, string,...
Описание слайда:
Resource management A resource should be owned by a “handle” A “handle” should present a well-defined and useful abstraction E.g. a vector, string, file, thread Use constructors and a destructor class Vector { // vector of doubles Vector(initializer_list); // acquire memory; initialize elements ~Vector(); // destroy elements; release memory // … private: double* elem; // pointer to elements int sz; // number of elements }; void fct() { Vector v {1, 1.618, 3.14, 2.99e8}; // vector of doubles // … }

Слайд 19


Resource management A handle usually is scoped Handles lifetime (initialization, cleanup), and more Vector::Vector(initializer_list lst) :elem {new...
Описание слайда:
Resource management A handle usually is scoped Handles lifetime (initialization, cleanup), and more Vector::Vector(initializer_list lst) :elem {new double[lst.size()]}, sz{lst.size()}; // acquire memory { uninitialized_copy(lst.begin(),lst.end(),elem); // initialize elements } Vector::~Vector() { delete[] elem; // destroy elements; release memory };

Слайд 20


Resource management What about errors? A resource is something you acquire and release A resource should have an owner Ultimately “root” a resource...
Описание слайда:
Resource management What about errors? A resource is something you acquire and release A resource should have an owner Ultimately “root” a resource in a (scoped) handle “Resource Acquisition Is Initialization” (RAII) Acquire during construction Release in destructor Throw exception in case of failure Can be simulated, but not conveniently Never throw while holding a resource not owned by a handle In general Leave established invariants intact when leaving a scope

Слайд 21


“Resource Acquisition is Initialization” (RAII) For all resources Memory (done by std::string, std::vector, std::map, …) Locks (e.g....
Описание слайда:
“Resource Acquisition is Initialization” (RAII) For all resources Memory (done by std::string, std::vector, std::map, …) Locks (e.g. std::unique_lock), files (e.g. std::fstream), sockets, threads (e.g. std::thread), … std::mutex mtx; // a resource int sh; // shared data void f() { std::lock_guard lck {mtx}; // grab (acquire) the mutex sh+=1; // manipulate shared data } // implicitly release the mutex

Слайд 22


Pointer Misuse Many (most?) uses of pointers in local scope are not exception safe void f(int n, int x) { Gadget* p = new Gadget{n}; // look I’m a...
Описание слайда:
Pointer Misuse Many (most?) uses of pointers in local scope are not exception safe void f(int n, int x) { Gadget* p = new Gadget{n}; // look I’m a java programmer!  // … if (x

Слайд 23


Resource Handles and Pointers A std::shared_ptr releases its object at when the last shared_ptr to it is destroyed void f(int n, int x) { shared_ptr...
Описание слайда:
Resource Handles and Pointers A std::shared_ptr releases its object at when the last shared_ptr to it is destroyed void f(int n, int x) { shared_ptr p {new Gadget{n}}; // manage that pointer! // … if (x

Слайд 24


Resource Handles and Pointers But why use a pointer at all? If you can, just use a scoped variable void f(int n, int x) { Gadget g {n}; // … if (x
Описание слайда:
Resource Handles and Pointers But why use a pointer at all? If you can, just use a scoped variable void f(int n, int x) { Gadget g {n}; // … if (x

Слайд 25


Why do we use pointers? And references, iterators, etc. To represent ownership Don’t! Instead, use handles To reference resources from within a...
Описание слайда:
Why do we use pointers? And references, iterators, etc. To represent ownership Don’t! Instead, use handles To reference resources from within a handle To represent positions Be careful To pass large amounts of data (into a function) E.g. pass by const reference To return large amount of data (out of a function) Don’t! Instead use move operations

Слайд 26


How to get a lot of data cheaply out of a function? Ideas Return a pointer to a new’d object Who does the delete? Return a reference to a new’d...
Описание слайда:
How to get a lot of data cheaply out of a function? Ideas Return a pointer to a new’d object Who does the delete? Return a reference to a new’d object Who does the delete? Delete what? Pass a target object We are regressing towards assembly code Return an object Copies are expensive Tricks to avoid copying are brittle Tricks to avoid copying are not general Return a handle Simple and cheap

Слайд 27


Move semantics Return a Matrix Matrix operator+(const Matrix& a, const Matrix& b) { Matrix r; // copy a[i]+b[i] into r[i] for each i return r; }...
Описание слайда:
Move semantics Return a Matrix Matrix operator+(const Matrix& a, const Matrix& b) { Matrix r; // copy a[i]+b[i] into r[i] for each i return r; } Matrix res = a+b; Define move a constructor for Matrix don’t copy; “steal the representation”

Слайд 28


Move semantics Direct support in C++11: Move constructor class Matrix { Representation rep; // … Matrix(Matrix&& a) // move constructor { rep =...
Описание слайда:
Move semantics Direct support in C++11: Move constructor class Matrix { Representation rep; // … Matrix(Matrix&& a) // move constructor { rep = a.rep; // *this gets a’s elements a.rep = {}; // a becomes the empty Matrix } }; Matrix res = a+b;

Слайд 29


No garbage collection needed For general, simple, implicit, and efficient resource management Apply these techniques in order: Store data in...
Описание слайда:
No garbage collection needed For general, simple, implicit, and efficient resource management Apply these techniques in order: Store data in containers The semantics of the fundamental abstraction is reflected in the interface Including lifetime Manage all resources with resource handles RAII Not just memory: all resources Use “smart pointers” They are still pointers Plug in a garbage collector For “litter collection” C++11 specifies an interface Can still leak non-memory resources

Слайд 30


Range-for, auto, and move As ever, what matters is how features work in combination template vector find_all(C& c, V v) // find all occurrences of v...
Описание слайда:
Range-for, auto, and move As ever, what matters is how features work in combination template vector find_all(C& c, V v) // find all occurrences of v in c { vector res; for (auto& x : c) if (x==v) res.push_back(&x); return res; } string m {"Mary had a little lamb"}; for (const auto p : find_all(m,'a')) // p is a char* if (*p!='a') cerr

Слайд 31


RAII and Move Semantics All the standard-library containers provide it vector list, forward_list (singly-linked list), … map, unordered_map (hash...
Описание слайда:
RAII and Move Semantics All the standard-library containers provide it vector list, forward_list (singly-linked list), … map, unordered_map (hash table),… set, multi_set, … … string So do other standard resources thread, lock_guard, … istream, fstream, … unique_ptr, shared_ptr …

Слайд 32


OOP
Описание слайда:
OOP

Слайд 33


Class hierarchies Protection model No universal base class an unnecessary implementation-oriented artifact imposes avoidable space and time...
Описание слайда:
Class hierarchies Protection model No universal base class an unnecessary implementation-oriented artifact imposes avoidable space and time overheads. encourages underspecified (overly general) interfaces Multiple inheritance Separately consider interface and implementation Abstract classes provide the most stable interfaces Minimal run-time type identification dynamic_cast(pb) typeid(p)

Слайд 34


Inheritance Warning: Inheritance has been seriously and systematically overused and misused “When your only tool is a hammer everything looks like a...
Описание слайда:
Inheritance Warning: Inheritance has been seriously and systematically overused and misused “When your only tool is a hammer everything looks like a nail”

Слайд 35


GP
Описание слайда:
GP

Слайд 36


Generic Programming: Templates 1980: Use macros to express generic types and functions 1987 (and current) aims: Extremely general/flexible “must be...
Описание слайда:
Generic Programming: Templates 1980: Use macros to express generic types and functions 1987 (and current) aims: Extremely general/flexible “must be able to do much more than I can imagine” Zero-overhead vector/Matrix/… to compete with C arrays Well-specified interfaces Implying overloading, good error messages, and maybe separate compilation “two out of three ain’t bad” But it isn’t really good either it has kept me concerned/working for 20+ years

Слайд 37


Templates Compile-time duck typing Leading to template metaprogramming A massive success in C++98, better in C++11, better still in C++14 STL...
Описание слайда:
Templates Compile-time duck typing Leading to template metaprogramming A massive success in C++98, better in C++11, better still in C++14 STL containers template class vector { /* … */ }; STL algorithms sort(v.begin(),v.end()); And much more Better support for compile-time programming C++11: constexpr (improved in C++14)

Слайд 38


Algorithms Messy code is a major source of errors and inefficiencies We must use more explicit, well-designed, and tested algorithms The C++...
Описание слайда:
Algorithms Messy code is a major source of errors and inefficiencies We must use more explicit, well-designed, and tested algorithms The C++ standard-library algorithms are expressed in terms of half-open sequences [first:last) For generality and efficiency void f(vector& v, list& lst) { sort(v.begin(),v.end()); // sort the vector using

Слайд 39


Algorithms Simple, efficient, and general implementation For any forward iterator For any (matching) value type template Iter find(Iter first, Iter...
Описание слайда:
Algorithms Simple, efficient, and general implementation For any forward iterator For any (matching) value type template Iter find(Iter first, Iter last, Value val) // find first p in [first:last) so that *p==val { while (first!=last && *first!=val) ++first; return first; }

Слайд 40


Algorithms and Function Objects Parameterization with criteria, actions, and algorithms Essential for flexibility and performance void g(vector<...
Описание слайда:
Algorithms and Function Objects Parameterization with criteria, actions, and algorithms Essential for flexibility and performance void g(vector< string>& vs) { auto p = find_if(vs.begin(), vs.end(), Less_than{"Griffin"}); // … }

Слайд 41


Algorithms and Function Objects The implementation is still trivial template Iter find_if(Iter first, Iter last, Predicate pred) // find first p in...
Описание слайда:
Algorithms and Function Objects The implementation is still trivial template Iter find_if(Iter first, Iter last, Predicate pred) // find first p in [first:last) so that pred(*p) { while (first!=last && !pred(*first)) ++first; return first; }

Слайд 42


Function Objects and Lambdas General function object Can carry state Easily inlined (i.e., close to optimally efficient) struct Less_than { String s;...
Описание слайда:
Function Objects and Lambdas General function object Can carry state Easily inlined (i.e., close to optimally efficient) struct Less_than { String s; Less_than(const string& ss) :s{ss} {} // store the value to compare against bool operator()(const string& v) const { return v

Слайд 43


Container algorithms The C++ standard-library algorithms are expressed in terms of half-open sequences [first:last) For generality and efficiency If...
Описание слайда:
Container algorithms The C++ standard-library algorithms are expressed in terms of half-open sequences [first:last) For generality and efficiency If you find that verbose, define container algorithms namespace Extended_STL { // … template Iterator find_if(C& c, Predicate pred) { return std::find_if(c.begin(),c.end(),pred); } // … } auto p = find_if(v, [](int x) { return x%2; } ); // assuming v is a vector

Слайд 44


Duck Typing is Insufficient There are no proper interfaces Leaves error detection far too late Compile- and link-time in C++ Encourages a focus on...
Описание слайда:
Duck Typing is Insufficient There are no proper interfaces Leaves error detection far too late Compile- and link-time in C++ Encourages a focus on implementation details Entangles users with implementation Leads to over-general interfaces and data structures As programmers rely on exposed implementation “details” Does not integrate well with other parts of the language Teaching and maintenance problems We must think of generic code in ways similar to other code Relying on well-specified interfaces (like OO, etc.)

Слайд 45


Generic Programming is just Programming Traditional code double sqrt(double d); // C++84: accept any d that is a double double d = 7; double d2 =...
Описание слайда:
Generic Programming is just Programming Traditional code double sqrt(double d); // C++84: accept any d that is a double double d = 7; double d2 = sqrt(d); // fine: d is a double double d3 = sqrt(&d); // error: &d is not a double Generic code void sort(Container& c); // C++14: accept any c that is a Container vector vs { "Hello", "new", "World" }; sort(vs); // fine: vs is a Container sort(&vs); // error: &vs is not a Container

Слайд 46


C++14: Constraints aka “Concepts lite” How do we specify requirements on template arguments? state intent Explicitly states requirements on argument...
Описание слайда:
C++14: Constraints aka “Concepts lite” How do we specify requirements on template arguments? state intent Explicitly states requirements on argument types provide point-of-use checking No checking of template definitions use constexpr functions Voted as C++14 Technical Report Design by B. Stroustrup, G. Dos Reis, and A. Sutton Implemented by Andrew Sutton in GCC There are no C++0x concept complexities No concept maps No new syntax for defining concepts No new scope and lookup issues

Слайд 47


What is a Concept? Concepts are fundamental They represent fundamental concepts of an application area Concepts are come in “clusters” describing an...
Описание слайда:
What is a Concept? Concepts are fundamental They represent fundamental concepts of an application area Concepts are come in “clusters” describing an application area A concept has semantics (meaning) Not just syntax “Subtractable” is not a concept We have always had concepts C++: Integral, arithmetic STL: forward iterator, predicate Informally: Container, Sequence Algebra: Group, Ring, …

Слайд 48


What is a Concept? Don’t expect to find a new fundamental concept every year A concept is not the minimal requirements for an implementation An...
Описание слайда:
What is a Concept? Don’t expect to find a new fundamental concept every year A concept is not the minimal requirements for an implementation An implementation does not define the requirements Requirements should be stable Concepts support interoperability There are relatively few concepts We can remember a concept

Слайд 49


C++14 Concepts (Constraints) A concept is a predicate on one or more arguments E.g. Sequence() // is T a Sequence? Template declaration template...
Описание слайда:
C++14 Concepts (Constraints) A concept is a predicate on one or more arguments E.g. Sequence() // is T a Sequence? Template declaration template requires Sequence() && Equality_comparable() Iterator_of find(S& seq, const T& value); Template use void use(vector& vs) { auto p = find(vs,"Jabberwocky"); // … }

Слайд 50


C++14 Concepts: Error handling Error handling is simple (and fast) template void sort(Cont& container); vector vec {1.2, 4.5, 0.5, -1.2}; list lst...
Описание слайда:
C++14 Concepts: Error handling Error handling is simple (and fast) template void sort(Cont& container); vector vec {1.2, 4.5, 0.5, -1.2}; list lst {1, 3, 5, 4, 6, 8,2}; sort(vec); // OK: a vector is Sortable sort(lst); // Error at (this) point of use: Sortable requires random access Actual error message error: ‘list’ does not satisfy the constraint ‘Sortable’

Слайд 51


C++14 Concepts: “Shorthand Notation” Shorthand notation template Iterator_of find(S& seq, const T& value); We can handle essentially all of the Palo...
Описание слайда:
C++14 Concepts: “Shorthand Notation” Shorthand notation template Iterator_of find(S& seq, const T& value); We can handle essentially all of the Palo Alto TR (STL algorithms) and more Except for the axiom parts We see no problems checking template definitions in isolation But proposing that would be premature (needs work, experience) We don’t need explicit requires much (the shorthand is usually fine)

Слайд 52


C++14 Concepts: Overloading Overloading is easy template Iterator_of find(S& seq, const T& value); template Iterator_type find(C& assoc, const...
Описание слайда:
C++14 Concepts: Overloading Overloading is easy template Iterator_of find(S& seq, const T& value); template Iterator_type find(C& assoc, const Key_type& key); vector v { /* ... */ }; multiset s { /* … */ }; auto vi = find(v, 42); // calls 1st overload: // a vector is a Sequence auto si = find(s, 12-12-12); // calls 2nd overload: // a multiset is an Associative_container

Слайд 53


C++14 Concepts: Overloading Overloading based on predicates specialization based on subset Far easier than writing lots of tests template void...
Описание слайда:
C++14 Concepts: Overloading Overloading based on predicates specialization based on subset Far easier than writing lots of tests template void advance(Iter& p, Difference_type n) { while (n--) ++p; } template void advance(Iter& i, Difference_type n) { if (n > 0) while (n--) ++p; if (n < 0) while (n++) --ip} template void advance(Iter& p, Difference_type n) { p += n; } We don’t say Input_iterator < Bidirectional_iterator < Random_access_iterator we compute it

Слайд 54


C++14 Concepts: Definition How do you write constraints? Any bool expression Including type traits and constexpr function a requires(expr) expression...
Описание слайда:
C++14 Concepts: Definition How do you write constraints? Any bool expression Including type traits and constexpr function a requires(expr) expression requires() is a compile time intrinsic function true if expr is a valid expression To recognize a concept syntactically, we can declare it concept Rather than just constexpr

Слайд 55


C++14 Concepts: “Terse Notation” We can use a concept name as the name of a type than satisfy the concept void sort(Container& c); // terse notation...
Описание слайда:
C++14 Concepts: “Terse Notation” We can use a concept name as the name of a type than satisfy the concept void sort(Container& c); // terse notation means template // shorthand notation void sort(__Cont& c); means template // explicit use of predicate requires Container() void sort(__Cont)& c; Accepts any type that is a Container vector vs; sort(vs);

Слайд 56


C++14 Concepts: “Terse Notation” We have reached the conventional notation with the conventional meaning Traditional code double sqrt(double d); //...
Описание слайда:
C++14 Concepts: “Terse Notation” We have reached the conventional notation with the conventional meaning Traditional code double sqrt(double d); // C++84: accept any d that is a double double d = 7; double d2 = sqrt(d); // fine: d is a double double d3 = sqrt(&d); // error: &d is not a double Generic code void sort(Container& c); // C++14: accept any c that is a Container vector vs { "Hello", "new", "World" }; sort(vs); // fine: vs is a Container sort(&vs); // error: &vs is not a Container

Слайд 57


C++14 Concepts: “Terse Notation” Consider std::merge Explicit use of predicates: template requires Forward_iterator() && Forward_iterator() &&...
Описание слайда:
C++14 Concepts: “Terse Notation” Consider std::merge Explicit use of predicates: template requires Forward_iterator() && Forward_iterator() && Output_iterator() && Assignable() && Assignable

Слайд 58


C++14 Concepts: “Terse Notation” Better, use the shorthand notation template requires Mergeable() void merge(For p, For q, For2 p2, For2 q2, Out p);...
Описание слайда:
C++14 Concepts: “Terse Notation” Better, use the shorthand notation template requires Mergeable() void merge(For p, For q, For2 p2, For2 q2, Out p); Quite readable

Слайд 59


C++14 Concepts: “Terse Notation” Better still, use the “terse notation”: Mergeable{For,For2,Out} // Mergeable is a concept requiring three types void...
Описание слайда:
C++14 Concepts: “Terse Notation” Better still, use the “terse notation”: Mergeable{For,For2,Out} // Mergeable is a concept requiring three types void merge(For p, For q, For2 p2, For2 q2, Out p); The concept-name { identifier-list } notation introduces constrained names Make simple things simple!

Слайд 60


C++14 Concepts: “Terse Notation” Now we just need to define Mergeable: template concept bool Mergeable() { return Forward_iterator() &&...
Описание слайда:
C++14 Concepts: “Terse Notation” Now we just need to define Mergeable: template concept bool Mergeable() { return Forward_iterator() && Forward_iterator() && Output_iterator() && Assignable() && Assignable

Слайд 61


Challenges
Описание слайда:
Challenges

Слайд 62


C++ Challenges Obviously, C++ is not perfect How can we make programmers prefer modern styles over low-level code which is far more error-prone and...
Описание слайда:
C++ Challenges Obviously, C++ is not perfect How can we make programmers prefer modern styles over low-level code which is far more error-prone and harder to maintain, yet no more efficient? How can we make C++ a better language given the Draconian constraints of C and C++ compatibility? How can we improve and complete the techniques and models (incompletely and imperfectly) embodied in C++? Solutions that eliminate major C++ strengths are not acceptable Compatibility link, source code Performance uncompromising Portability Range of application areas Preferably increasing the range

Слайд 63


Long-term C++ Challenges Close more type loopholes in particular, find a way to prevent misuses of delete without spoiling RAII Simplify concurrent...
Описание слайда:
Long-term C++ Challenges Close more type loopholes in particular, find a way to prevent misuses of delete without spoiling RAII Simplify concurrent programming in particular, provide some higher-level concurrency models as libraries Simplify generic programming in particular, introduce simple and effective concepts Simplify programming using class hierarchies in particular, eliminate use of the visitor pattern Better support for combinations of object-oriented and generic programming Make exceptions usable for hard-real-time projects that will most likely be a tool rather than a language change Find a good way of using multiple address spaces as needed for distributed computing would probably involve defining a more general module mechanism that would also address dynamic linking, and more. Provide many more domain-specific libraries Develop a more precise and formal specification of C++

Слайд 64


“Paradigms” Much of the distinction between object-oriented programming, generic programming, and “conventional programming” is an illusion based on...
Описание слайда:
“Paradigms” Much of the distinction between object-oriented programming, generic programming, and “conventional programming” is an illusion based on a focus on language features incomplete support for a synthesis of techniques The distinction does harm by limiting programmers, forcing workarounds void draw_all(Container& c) // is this OOP, GP, or conventional? requires Same_type { for_each(c, [](Shape* p) { p->draw(); } ); }

Слайд 65


Questions?
Описание слайда:
Questions?



Теги Essence with examples
Похожие презентации
Mypresentation.ru
Загрузить презентацию