[](){}(); C++11 as a new language Thiago Macieira OSCON 2012
Who am I? • Open Source developer for over 15 years Developing with C++ since 2000 • Working for Intel’s Open Source Technology Center • Maintainer of modules in the Qt* open source project • MBA and double degree in Engineering • Collect way too many air miles presenting in events * Other names and brands may be claimed as property of others INTEL CONFIDENTIAL 2
Status of C++11 • Approved by ISO on August 2011 • Published as ISO/IEC 14882:2011 • Partial support in most compilers • Portions of it incorporated into ISO/IEC 9899:2011 – that is, C11 INTEL CONFIDENTIAL 3
Native matters 4
C and C++ are very widespread • Operating systems • Middleware layers • High-end games C C++ Python PHP Ruby WebKit JRE’s JVM Gecko V8 INTEL CONFIDENTIAL 5
Native development still crucial • Performance, performance, performance – “Bare metal”, no compromises • Accessing devices 1000 Multiples of fastest program • Resource-limited environments 100 10 1 C C++ Java 7 JavaScript V8 Ruby 1.9 PHP Python 3 INTEL CONFIDENTIAL 6 Source: “The Computer Language Benchmarks Game”, http://shootout.alioth.debian.org/
Also reflected in the job market • Maintenance • New development INTEL CONFIDENTIAL 7 Source: indeed.com
Will continue expanding • User interfaces getting more complex • Embedded market growing • Existing installations requiring maintenance and improvements Arm wrestling between hardware and UX requirements INTEL CONFIDENTIAL 8
Why change? • What’s wrong with C99 and C++98? INTEL CONFIDENTIAL 9
C++11 goals • Evolve the language • Keep compatibility with C++98 and C • Prioritise library and system design • Improve type-safety techniques • Improve concurrency / threading support INTEL CONFIDENTIAL 10
New features 11
What’s new in C++11? Raw strings Unrestricted unions std::tuple Rvalue references Static assertions std::shared_ptr Template alias thread_local enum class decltype nullptr Explicit overrides Lambdas User-defined literals Trivial types std::regex Standard layout types Defaulted & deleted members Type traits Unicode strings Variadic templates Initialiser lists Uniform initialisation auto types Extern templates std::function alignof and alignas constexpr Range-based for Delegating constructors Move semantics Inheriting constructors Memory model >> Atomic types INTEL CONFIDENTIAL 12 Entries in bold are also in C11
Atomics and memory model • Improving support for multi-threaded programs: – No more poorly-defined “sequence points” in the standard – The standard now talks about “happens before” – Low-level atomic operations library • Both features in C11 too • Lock-free programming without assembly INTEL CONFIDENTIAL 13
constexpr • Under strict rules, allows the compiler to “run” the code • A new way of meta-programming template<int N> struct Factorial { constexpr int factorial(int n) enum { { Result = N * Factorial<N - 1>::Result return n == 0 ? 1 : }; n * factorial(n - 1); }; } template<> struct Factorial<0> { enum { Result = 1 }; }; char array1[Factorial<4>::Result]; char array2[factorial(4)]; INTEL CONFIDENTIAL 14
Lambdas • First-class citizens in C++11 • Improves readability by keeping code close by. • Syntax: auto f = [ captures go here ]( parameters go here ) { code goes here } – Special captures: = captures the entire scope by value & captures the entire scope by reference INTEL CONFIDENTIAL 15
Modern strings • This is 2012, we have Unicode: const char16_t greek[] = u"Γεια σου, κόσμε!"; const char32_t russian[] = U"Здравствуй, мир!"; • Criticism: no standard marker for source code encoding INTEL CONFIDENTIAL 16
Compiler support for C++11 • GCC 4.7: best support http://gcc.gnu.org/projects/cxx0x.html • Clang 3.1: like GCC http://clang.llvm.org/cxx_status.html • Intel CC 12.1: decent support http://software.intel.com/en-us/articles/c0x-features-supported-by-intel-c-compiler • Microsoft Visual Studio 2010: lagging behind (situation improves with 2012) • More information: http://wiki.apache.org/stdcxx/C++0xCompilerSupport INTEL CONFIDENTIAL 17
Go beyond 18
Example: threading High-level: Low-level: • Cross-platform API for • Lock-free programming threads and mutexes • Memory model makes it clear • Known behaviour what can or can’t happen – Compiler optimisations • No third-party frameworks INTEL CONFIDENTIAL 19
Example: type traits • New type definitions: – Standard layout – Trivial • Allows generic containers to optimise operations • memcpy & realloc INTEL CONFIDENTIAL 20
Example: type traits & move semantics • New type definitions: • “Temporaries” can be modified – Standard layout – Trivial • Avoids copying unnecessarily • Huge library of traits, like – is_enum, is_function, is_pod – is_polymorphic, is_abstract More efficient containers – is_nothrow_constructible, has_virtual_destructor INTEL CONFIDENTIAL 21
Why this is important for open source? • Fewer occasions for More readable code undefined behaviour • Incremental improvements Better code for existing code Fewer bugs Happy users! INTEL CONFIDENTIAL 22
Thank you • Questions? • This presentation will be available at: http://oscon.com/slides • thiago.macieira@intel.com INTEL CONFIDENTIAL 23
Recommend
More recommend