[](){}(); C++11 as a new language Thiago Macieira OSCON 2012 Who - - PowerPoint PPT Presentation
[](){}(); C++11 as a new language Thiago Macieira OSCON 2012 Who - - PowerPoint PPT Presentation
[](){}(); 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 Intels Open Source Technology Center Maintainer of modules in the Qt*
INTEL CONFIDENTIAL 2
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 3
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
Native matters
4
INTEL CONFIDENTIAL 5
C and C++ are very widespread
- Operating systems
- Middleware layers
- High-end games
C++
PHP WebKit Gecko V8
C
Python Ruby JRE’s JVM
INTEL CONFIDENTIAL 6
Native development still crucial
- Performance, performance, performance
– “Bare metal”, no compromises
- Accessing devices
- Resource-limited
environments
C C++ Java 7 JavaScript V8 Ruby 1.9 PHP Python 3 1 10 100 1000 Source: “The Computer Language Benchmarks Game”, http://shootout.alioth.debian.org/
Multiples of fastest program
INTEL CONFIDENTIAL 7
Also reflected in the job market
Source: indeed.com
- Maintenance
- New development
INTEL CONFIDENTIAL 8
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 9
Why change?
- What’s wrong with C99 and C++98?
INTEL CONFIDENTIAL 10
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
New features
11
INTEL CONFIDENTIAL 12
What’s new in C++11?
Rvalue references Move semantics constexpr Trivial types Standard layout types Extern templates Initialiser lists Uniform initialisation auto types decltype Range-based for Lambdas Delegating constructors Inheriting constructors Explicit overrides nullptr enum class Template alias Unrestricted unions Variadic templates Unicode strings User-defined literals Memory model Defaulted & deleted members Static assertions alignof and alignas Atomic types std::tuple std::regex std::shared_ptr std::function Type traits Raw strings thread_local >>
Entries in bold are also in C11
INTEL CONFIDENTIAL 13
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 14
constexpr
template<int N> struct Factorial { enum { Result = N * Factorial<N - 1>::Result }; }; template<> struct Factorial<0> { enum { Result = 1 }; }; char array1[Factorial<4>::Result]; constexpr int factorial(int n) { return n == 0 ? 1 : n * factorial(n - 1); } char array2[factorial(4)];
- Under strict rules, allows the compiler to “run” the code
- A new way of meta-programming
INTEL CONFIDENTIAL 15
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 16
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 17
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
Go beyond
18
INTEL CONFIDENTIAL 19
Example: threading
High-level:
- Cross-platform API for
threads and mutexes
- Known behaviour
- No third-party frameworks
Low-level:
- Lock-free programming
- Memory model makes it clear
what can or can’t happen
– Compiler optimisations
INTEL CONFIDENTIAL 20
Example: type traits
- New type definitions:
– Standard layout – Trivial
- Allows generic containers to optimise operations
- memcpy & realloc
INTEL CONFIDENTIAL 21
Example: type traits & move semantics
- New type definitions:
– Standard layout – Trivial
- Huge library of traits, like
– is_enum, is_function, is_pod – is_polymorphic, is_abstract – is_nothrow_constructible, has_virtual_destructor
- “Temporaries” can be
modified
- Avoids copying unnecessarily
More efficient containers
INTEL CONFIDENTIAL 22
Why this is important for open source?
- Fewer occasions for
undefined behaviour
- Incremental improvements
for existing code More readable code Better code Fewer bugs Happy users!
INTEL CONFIDENTIAL 23
Thank you
- Questions?
- This presentation will be available at:
http://oscon.com/slides
- thiago.macieira@intel.com