[](){}(); C++11 as a new language Thiago Macieira OSCON 2012 Who - - PowerPoint PPT Presentation

c 11 as a new language
SMART_READER_LITE
LIVE PREVIEW

[](){}(); 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*


slide-1
SLIDE 1

[](){}(); C++11 as a new language

Thiago Macieira OSCON 2012

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Native matters

4

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

INTEL CONFIDENTIAL 7

Also reflected in the job market

Source: indeed.com

  • Maintenance
  • New development
slide-8
SLIDE 8

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

slide-9
SLIDE 9

INTEL CONFIDENTIAL 9

Why change?

  • What’s wrong with C99 and C++98?
slide-10
SLIDE 10

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
slide-11
SLIDE 11

New features

11

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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
slide-14
SLIDE 14

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
slide-15
SLIDE 15

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

slide-16
SLIDE 16

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
slide-17
SLIDE 17

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
slide-18
SLIDE 18

Go beyond

18

slide-19
SLIDE 19

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

slide-20
SLIDE 20

INTEL CONFIDENTIAL 20

Example: type traits

  • New type definitions:

– Standard layout – Trivial

  • Allows generic containers to optimise operations
  • memcpy & realloc
slide-21
SLIDE 21

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

slide-22
SLIDE 22

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!

slide-23
SLIDE 23

INTEL CONFIDENTIAL 23

Thank you

  • Questions?
  • This presentation will be available at:

http://oscon.com/slides

  • thiago.macieira@intel.com
slide-24
SLIDE 24