and the Unified Universe

Similar documents
Metaclasses: Generative C++

Metaclasses: Generative C++

Debug C++ Without Running. Anastasia Kazakova

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

C++11/14 Rocks. Clang Edition. Alex Korban

Object-Oriented Principles and Practice / C++

CS

Advanced Systems Programming

Instantiation of Template class

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Lambda Correctness and Usability Issues

Overview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43

Objects Managing a Resource

Object-Oriented Principles and Practice / C++

Short Notes of CS201

Implicit Evaluation of auto Variables and Arguments

C++14 Reflections Without Macros, Markup nor External Tooling

pointers & references

CS201 - Introduction to Programming Glossary By

Advanced C++ Topics. Alexander Warg, 2017

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

Fixing Atomic Initialization, Rev1

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

Introduction to C++11 and its use inside Qt

COMP6771 Advanced C++ Programming

G52CPP C++ Programming Lecture 13

Technical Specification: Concepts

An Introduction to C++

Adding attribute reflection to C++.

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

ECE 3574: Dynamic Polymorphism using Inheritance

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

September 10,

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Lambda functions. Zoltán Porkoláb: C++11/14 1

Introduction To C#.NET

CPSC 427: Object-Oriented Programming

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

EL2310 Scientific Programming

And Even More and More C++ Fundamentals of Computer Science

CSI33 Data Structures

Making New Pseudo-Languages with C++

Coroutine concepts and metafunctions

Increases Program Structure which results in greater reliability. Polymorphism

C++ Constructor Insanity

C The new standard

C++ Templates. David Camp

async and ~future (Revision 3)

C++11 Introduction to the New Standard. Alejandro Cabrera February 1, 2012 Florida State University Department of Computer Science

A polymorphic value-type for C++

Special Member Functions

Part X. Advanced C ++

template<typename T> cout << "The value is << value << endl; } void printdata(t value){

More Improvements to std::future<t> - Revision 1

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

VIRTUAL FUNCTIONS Chapter 10

C++ Programming Lecture 7 Software Engineering Group

Item 4: Extensible Templates: Via Inheritance or Traits?

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

C++ (classes) Hwansoo Han

Object-Oriented Programming for Scientific Computing

IBM Rational Rhapsody TestConductor Add On. Code Coverage Limitations

The Pivot framework: Design and Implementation

Using Enum Structs as Bitfields

Variant: a type-safe union without undefined behavior (v2).

Inheritance, and Polymorphism.

COMP6771 Advanced C++ Programming

eingebetteter Systeme

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Part III. Advanced C ++

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

CPSC 427: Object-Oriented Programming

Override Control Using Contextual Keywords

Pointers. Developed By Ms. K.M.Sanghavi

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Modernizing legacy C++ code

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Object-Oriented Programming, Iouliia Skliarova

Expansion statements. Version history. Introduction. Basic usage

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

C++11/14 Rocks. VS2013 Edition. Alex Korban

Apply the following edits to N4741, the working draft of the Standard. The feature test macros cpp_lib_latch and cpp_lib_barrier should be created.

Simplifying C++0x Concepts

C++ Inheritance and Encapsulation

September 19,

Basic Types, Variables, Literals, Constants

An introduction to. Templates. Generic Programming. Good old C. Metaprogramming 4/13/2017

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Variant: a typesafe union. ISO/IEC JTC1 SC22 WG21 N4218

Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; }

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Properties of an identifier (and the object it represents) may be set at

aerix consulting C++11 Library Design Lessons from Boost and the Standard Library

Chapter 15. Object-Oriented Programming

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

y

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Transcription:

Herb Sutter

2

Bja rne and the Unified Universe

The C++ type system is unified! CORBA interface dynamic any variant property iterator/ range container COM class traits type flag enum enum class functor/ callable value/ Regular expr. template SI base class MI base class POD value struct

The C++ type system is unified! CORBA interface dynamic any variant property iterator/ range container COM class traits type flag enum enum class functor/ callable value/ Regular expr. template SI base class MI base class POD value struct

Source code class Point { int x, y; struct MyClass : Base { void f() { /*...*/ //... Compiler for (m : members) if (!v.has_access()) if(is_class()) v.make_private(); else // is_struct() v.make_public(); for (f : functions) { if (f.is_virtual_in_base_class() &&!f.is_virtual()) f.make_virtual(); if (!f.is_virtual_in_base_class() && f.specified_override()) ERROR( does not override ); if (f.is_destructor()) if (members_dtors_noexcept()) f.make_noexcept(); Definition class Point { private: int x, y; public: Point() =default; ~Point() noexcept =default; Point(const Point&) =default; Point& operator=(const Point&) =default; Point(Point&&) =default; Point& operator=(const Point&&) =default; class MyClass : public Base { public: virtual void f() { /*...*/ //... 6

Source code class Point { int x, y; struct MyClass : Base { void f() { /*...*/ //... Compiler Q: What if you could write your own code here, and give a name to a group of defaults & behaviors? (treat it as ordinary code, share it as a library, etc.) Definition class Point { private: int x, y; public: Point() =default; ~Point() noexcept =default; Point(const Point&) =default; Point& operator=(const Point&) =default; Point(Point&&) =default; Point& operator=(const Point&&) =default; class MyClass : public Base { public: virtual void f() { /*...*/ //... 7

Source code class Point { int x, y; struct MyClass : Base { void f() { /*...*/ //... Compiler Q: What if you could write your own code here, and give a name to a group of defaults & behaviors? (treat it as ordinary code, share it as a library, etc.) Definition class Point { private: int x, y; public: Point() =default; ~Point() noexcept =default; Point(const Point&) =default; Point& operator=(const Point&) =default; Point(Point&&) =default; Point& operator=(const Point&&) =default; class MyClass : public Base { public: virtual void f() { /*...*/ //... 8

$class denotes a metaclass. namespace std::experimental { // for illustration $class interface { /*...public pure virtual fns only + by default...*/ more specific than class interface Shape { /*... public virtual enforced + default...*/ Typical uses: Enforce rules (e.g., all functions must be public and virtual ) Provide defaults (e.g., functions are public and virtual by default ) Provide implicitly generated functions (e.g., has virtual destructor by default, has full comparison operators and default memberwise implementations ) 9

C++17 class Shape { public: virtual int area() const =0; virtual void scale_by(double factor) =0; virtual ~Shape() noexcept { Proposed interface Shape { int area() const; void scale_by(double factor); // careful not to write a nonpublic or // nonvirtual function, or a copy/move // operation, or a data member; no // enforcement under maintenance default + enforce: all public pure virtual functions enforce: no data members, no copy/move 10

$class interface { ~interface() noexcept { constexpr { compiler.require($interface.variables().empty(), "interfaces may not contain data members"); for (auto f : $interface.functions()) { compiler.require(!f.is_copy() &&!f.is_move(), "interfaces may not copy or move; consider a virtual clone()"); if (!f.has_access()) f.make_public(); compiler.require(f.is_public(), "interface functions must be public"); f.make_pure_virtual(); 11

$class interface { ~interface() noexcept { constexpr { compiler.require($interface.variables().empty(), "interfaces may not for each contain function data in the members"); instantiating class for (auto f : $interface.functions()) enforce { constraints, integrated with compiler messages compiler.require(!f.is_copy() &&!f.is_move(), "interfaces may not copy or move; apply defaults consider where a not virtual specified clone()"); by the user if (!f.has_access()) f.make_public(); compiler.require(f.is_public(), "interface define functions a type metaprogram must be public"); runs here $class metaclass f.make_pure_virtual(); interface Shape { int area() const; void scale_by(double factor); pair<int,int> get_extents() const; 12

$class interface { ~interface() noexcept { constexpr { compiler.require($interface.variables().empty(), "interfaces may not contain data members"); for (auto f : $interface.functions()) { Look ma, compiler.require(!f.is_copy() no standardese! &&!f.is_move(), Define language-like features using the language itself if (!f.has_access()) can read the source code f.make_public(); to language features like we can read + no loss in usability, expressiveness, diagnostics, performance, "interfaces may not copy or move; consider a virtual even compared clone()"); to other languages that added compiler.require(f.is_public(), "interface functions this must as be a built-in public"); the source f.make_pure_virtual(); code to STL and other libs interface Shape { language feature Bonus: Does my spec have a bug? Unit-test int area() const; and debug it as usual it s just code void scale_by(double factor); We do not have unit testing and debugging pair<int,int> get_extents() const; for standardese 13

C# language: ~18pg, English Proposed C++: ~10 lines, testable code $class interface { ~interface() noexcept { constexpr { compiler.require($interface.variables().empty(), "interfaces may not contain data members"); for (auto f : $interface.functions()) { compiler.require(!f.is_copy() &&!f.is_move(), "interfaces may not copy or move; " "consider a virtual clone()"); if (!f.has_access()) f.make_public(); compiler.require(f.is_public(), "interface functions must be public"); f.make_pure_virtual(); 14

C#, Java interface Shape { int area(); void scale_by(double factor); //... Proposed C++ interface Shape { int area() const; void scale_by(double factor); //... 15

C++17 class Point { int x = 0, y = 0; public: Point(int, int); //... behavior functions... Point() = default; friend bool operator==(const Point& a, const Point& b) { return a.x == b.x && a.y == b.y; friend bool operator!=(const Point& a, const Point& b) { return!(a == b); friend bool operator< (const Point& a, const Point& b) { return a.x < b.x (a.x == b.x && a.y < b.y); friend bool operator> (const Point& a, const Point& b) { return b < a; friend bool operator>=(const Point& a, const Point& b) { return!(a < b); friend bool operator<=(const Point& a, const Point& b) { return!(b < a); Proposed value Point { int x = 0, y = 0; Point(int, int); //... behavior functions... default + enforce: copy/move, comparisons, default ctor default (opt): private data, public functions enforce: no virtual functions 16

$class basic_value { basic_value() = default; basic_value(const basic_value& that) = default; basic_value(basic_value&& that) = default; basic_value& operator=(const basic_value& that) = default; basic_value& operator=(basic_value&& that) = default; constexpr { for (auto f : $basic_value.variables()) if (!f.has_access()) f.make_private(); for (auto f : $basic_value.functions()) { if (!f.has_access()) f.make_public(); compiler.require(!f.is_protected(), "a value type may not have a protected function"); compiler.require(!f.is_virtual(), "a value type may not have a virtual function"); compiler.require(!f.is_destructor() f.is_public(), "a value destructor must be public"); $class value : basic_value, ordered { 17

$class basic_value { basic_value() = default; basic_value(const basic_value& that) = default; basic_value(basic_value&& that) Point p(50, 100), = default; p2; basic_value& operator=(const basic_value& that) = default; basic_value& operator=(basic_value&& that) = default; constexpr { for (auto f : $basic_value.variables()) if (!f.has_access()) set<point> f.make_private(); s; value Point { int x = 0, y = 0; Point(int, int); // ok, default constructible p2 = get_some_point(); // ok, copyable if (p == p2) { /* */ // ok, == available // ok, < available for (auto f : $basic_value.functions()) { if (!f.has_access()) f.make_public(); compiler.require(!f.is_protected(), "a value type may not have a protected function"); compiler.require(!f.is_virtual(), "a value type may not have a virtual function"); compiler.require(!f.is_destructor() f.is_public(), "a value destructor must be public"); ordered provides <, >, <=, >=, ==,!= $class value : basic_value, ordered { 18

Qt moc Proposed.h with extensions moc compiler.h generated moc_*.cpp.cpp.cpp C++ compiler C++ compiler 19

Today (separate YAML script) ExampleHit : Description : "Example Hit" Author : "B. Hegner" Members: - double x // x-coordinate - double y // y-coordinate - double z // z-coordinate - double energy // measured generate: 5 interrelated classes X, XCollection, XConst, XData, XObj how: separate code generator 20

Today (separate YAML script) ExampleHit : Description : "Example Hit" Author : "B. Hegner" Members: - double x // x-coordinate - double y // y-coordinate - double z // z-coordinate - double energy // measured Proposed C++ (strawman) podio::datatype ExampleHit { string Description = "Example Hit"; string Author = "B. Hegner"; double x; // x-coordinate double y; // y-coordinate double z; // z-coordinate double energy; // measured generate: 5 interrelated classes X, XCollection, XConst, XData, XObj how: separate code generator default + enforce: constexpr static strings generate: same 5 classes how: during normal C++ compilation 21

Expand C++ s abstraction vocabulary beyond class/struct/union/enum Enable writing compiler-enforced coding standards, hardware interface patterns, etc. Enable writing language extensions as library code, with equal usability & efficiency Incl. valuable extensions we d never standardize in the language because they re too narrow (e.g., interface) Eliminate the need for side languages & compilers (e.g., Qt moc, COM IDL/MIDL, C++/CX) Benefits for users Don t have to wait for a new compiler Can share new language features as libraries Can even add productivity features themselves Benefits for standardization More features as libraries easier evolution Testable code higher-quality proposals Benefits for C++ implementations < new language features < compiler work Can deprecate and remove classes of extensions 22

Questions?