The Design of C++0x: Bjarne Stroustrup
The Design of C++0x: Bjarne Stroustrup
Bjarne Stroustrup
Texas A&M University
http://www.research.att.com/~bs
Caveat
There are people who want just the facts, just the technical details
Im writing the C++0x FAQ for you If you really want all the details, read the draft standard (hard) Technical details in isolation are sterile
There are people who want grand theory, fundamental principles, and no distracting details
I dont do that Theory in isolation is sterile
This talk
Gives a bit of background (history) and some simple design principles illustrated by the simplest code examples I can find
Stroustrup - CERN 2009 3
Overview
Aims, Ideals, and history C++ Design rules for C++0x
With examples
Case studies
Initialization Concurrency
Ada
Object Pascal
Ada95 C89/99
C++
Simula ML Lisp Smalltalk Java
C++0x
C#
Programming languages
A programming language exists to help people express ideas Programming language features exist to serve design and programming techniques The primary value of a programming language is in the applications written in it
The quest for better languages has been long and continues
Stroustrup - CERN 2009 6
Assembler 1951
Machine code to assembler and libraries
Abstraction Efficiency Testing documentation
Fortran 1956
A notation fit for humans
For a specific application domain
A(I) = B(I)+C*D(I)
Simula 1967
Organize code to model the real world
Object-oriented design
C 1974
An simple and general notation for systems programming
Somewhat portable Direct mapping of objects and basic operations to machine
Performance becomes somewhat portable
10
11
Non-proprietary
Yet almost universally supported ISO standard from 1998
User-defined types receive the same support as built-in types Standard library written in the language itself
And most non-standard libraries
Stroustrup - CERN 2009 13
C++ is everywhere
http://www.research.att.com/~bs/applications.html
Telecommunications Google Microsoft applications and GUIs Linux tools and GUIs Games PhotoShop Finance Mars Rovers Marine diesel engines Cell phones Human genome project Micro electronics design and manufacturing
14
40 to 100 at a meeting
~60 currently
Design?
Can a committee design?
No (at least not much) Few people consider or care for the whole language
Is C++0x designed
Yes
Well, mostly You can see traces of different personalities in C++0x
Committees
Discuss Bring up problems Polish
17
Template meta-programming!
What is C++?
A hybrid language A multi-paradigm programming language Its C! Embedded systems programming language Supports generic programming A random collection 19 of features
Buffer overflows
Too big!
C++0x
It feels like a new language
Compared to C++98
The pieces (language features) fit together much better than they used to Stroustrup - CERN 2009 20
C++
C++ emphasis
Flexible static type system Performance (in time and space) Small abstractions
22
24
26
std::unordered_map, std::regex,
Not built-in associative array
29
Example: Education
Teachers, courses, and textbooks
Often mired in 1970s thinking (C is the perfect language) or 1980s thinking (OOP Rah Rah Rah)
Etc.
static_assert improved enums long long, C99 character types, etc. Stroustrup - CERN 2009
33
Case studies
Concurrency
driven by necessity
Initialization
language maintenance
34
35
Concurrency
Not
Massively parallel (scientific) computing Web services Simple high-level abstract model System of real-time guarantees
Instead
A systems-level foundation for all
Stroustrup - CERN 2009 36
Concurrency overview
Foundation
Memory model atomics
Resource management
std::unique_ptr, std::shared_ptr Stroustrup - CERN 2009 GC ABI
37
Memory model
A memory model is an agreement between the machine architects and the compiler writers to ensure that most programmers do not have to think about the details of modern computer hardware.
// thread 1: char c; c = 1; int x = c; // thread 2: char b; b = 1; int y = b;
x==1 and y==0 as anyone would expect (but dont try that for two bitfields of the same word)
38
enum memory_order { // regular (non-atomic) memory synchronization order memory_order_relaxed, memory_order_consume, memory_order_acquire, memory_order_release, memory_order_acq_rel, memory_order_seq_cst }; C atomic_load_explicit(const volatile A* object, memory_order); void atomic_store_explicit(volatile A *object, C desired, memory_order order); bool atomic_compare_exchange_weak_explicit(volatile A* object, C * expected, C desired, memory_order success, memory_order failure); Stroustrup - CERN 2009 39 // lots more
Concurrency: std::thread
#include<thread> void f() { std::cout << "Hello "; struct F { void operator()() { std::cout << "parallel world "; } }; int main() { std::thread t1{f}; // f() executes in separate thread std::thread t2{F()}; // F()() executes in separate thread } // spot the bugs
40
Concurrency: std::thread
int main() { std::thread t1{f}; std::thread t2{F()}; t1.join(); t2.join(); } // and another bug: dont write to cout without synchronization // f() executes in separate thread // F()() executes in separate thread
41
// Note: time
43
45
result
future+promise provides a simple way of passing a value from one thread to another
No explicit synchronization Exceptions can be transmitted between threads
Stroustrup - CERN 2009 46
Put to a promise:
try { X res; // compute a value for res p.set_value(res); } catch (...) { // oops: couldn't compute res p.set_exception(std::current_exception()); }
Stroustrup - CERN 2009 47
async()
Simple launcher (warning: only approved in principle)
template<class T, class V> struct Accum { // accumulator function object }; void comp(vector<double>& v) // spawn many tasks if v is large enough { if (v.size()<10000) return std::accumulate(v.begin(),v.end(),0.0); auto f0 = async(Accum{&v[0],&v[v.size()/4],0.0}); auto f1 = async(Accum{&v[v.size()/4],&v[v.size()/2],0.0}); auto f2 = async(Accum{&v[v.size()/2],&v[v.size()*3/4],0.0}); auto f3 = async(Accum{&v[v.size()*3/4],&v[v.size()],0.0}); return f0.get()+f1.get()+f2.get()+f3.get(); } Stroustrup - CERN 2009 48
Future
Lots of use
C++98, C++0x, C++1x,
yes
Challenges
Small language (or at least much, much smaller) Complete and enforced type safety Concurrency
Stroustrup - CERN 2009
49
Thanks!
C and Simula
Brian Kernighan Doug McIlroy Kristen Nygaard Dennis Ritchie Steve Clamage Francis Glassborow Andrew Koenig Tom Plum Herb Sutter
Application builders
Stroustrup - CERN 2009 50
More information
My HOPL-II and HOPL-III papers The Design and Evolution of C++ (Addison Wesley 1994) My home pages
Papers, FAQs, libraries, applications, compilers,
Search for Bjarne or Stroustrup
C++0x FAQ
C++0x examples
// bind a template argument (Currying): template<class T> using Vec = std::vector<T,My_alloc<T>>; // an alias Vec<double> v = { 1, 2.2, 3, 9 }; // Note: general and uniform initialization sort(v); // simplicity is the ultimate sophistication (and no spurious overheads) sort({"Nygaard", "Ritchie", "Richards"}); // error: can sort a constant for (auto x : v ) cout << x <<'\n'; // simple traversal // run in parallel: auto x = asynch([&v]() { return accumulate(v.begin(), v.end(), 0.0); }); // a lambda // double d = x.get(); // if necessary, wait for result
Stroustrup - CERN 2009 52
C++0x examples
struct F { // function object F(const string&); double operator()(double, int); // application (function call) operator // }; auto f = bind(F("Hello"), _1, 42); // _1 is a placeholder double dd = f(1.23); // F(hello)(1.23,42);
53
Principle violated:
Uniform support for types (user-defined and built-in)
56
Principle violated:
Support fundamental notions directly (state intent)
57
Principle violated:
Support user-defined and built-in types equally well
58
void f(int, std::initializer_list<int>, int); f(1, {2,3,4}, 5); f(42, {1,a,3,b,c,d,x+y,0,g(x+a),0,0,3}, 1066);
60
X { } is always an initialization
X var{} // no operand; default initialization
Not a function definition like X var();
Solution:
C++0x { } initialization doesnt narrow.
all examples above are caught
63
Uniform Initialization
Example
Table phone_numbers = { { "Donald Duck", 2015551234 }, { Mike Doonesbury", 9794566089 }, { "Kell Dewclaw", 1123581321 } };
What is Table?
a map? An array of structs? A vector of pairs? My own class with a constructor? A struct needing aggregate initialization? Something else? We dont care as long as it can be constructed using a C-style string and an integer. Those numbers cannot get truncated
64