Vous êtes sur la page 1sur 16

Applied C++11

2013-01-25 Fri

Outline

Introduction

Auto-Type Inference

Lambda Functions

Threading

Compiling

C++11

I C++11 (formerly known as C++0x) is the most recent


version of the standard of the C++

I Approved by ISO(International Organization for


Standardization) on 12 August 2011

I Includes several additions to the core language and


extends the C++ standard library

Core language usability enhancements:

I Primary purpose is to make the language easier to use


I Improve type safety
I Minimize code repetition
I Make erroneous code less likely

Auto Type Inference

I The compiler can infer the type of a variable at the point


of declaration:

int x = 5 ;
auto x = 4 ;
I Very helpful when working with templates and iterators:

int

vector <
> vec ;
i t r = vec . i t e r a t o r ( ) ;

auto
//

instead

of

v e c t o r <i n t > : : i t e r a t o r

itr

Study Case
t e m p l a t e <typename B u i l t T y p e , typename B u i l d e r >

void

const

makeAndProcessObject (
B u i l d e r& b u i l d e r )
{
B u i l t T y p e v a l = b u i l d e r . makeObject ( ) ;
//

do

stuff

with

val

}
two necessary template parameters

I type of the builder object


I type of the object being built

MyObjBuilder b u i l d e r ;
ma keA ndP roc ess Ob ject <MyObj>( b u i l d e r ) ;

Study Case (cont.)

I By using `auto' no longer need to write the specic type


of the built object

t e m p l a t e <typename B u i l d e r >

void

const

makeAndProcessObject (
B u i l d e r& b u i l d e r )
{
v a l = b u i l d e r . makeObject ( ) ;

auto

//

do

stuff

with

val

}
MyObjBuilder b u i l d e r ;
makeAndProcessObject ( b u i l d e r ) ;

Lambda Functions

I Lambda Function: is a function dened, and possibly


called, without being bound to an identier

I you can write inline in your source code


I convenient to pass as an argument to a higher-order
function

I allow to create entirely new ways of writing programs

Basic Lambda Syntax

#include <i o s t r e a m >


u s i n g namespace s t d ;
int main ( )
{
}

[ ] ( ) { c o u t << " H e l l o , World ! " ; } ( ) ;

I []: capture specier, species for the compiler creation


of a lambda function

I (): argument list


I What about return values?

More on the Lambda Syntax

I If there is no return statement, uses void as default


I If you have a simple return expression, the compiler will
deduce the type of the return value
//

return

compiler

[ ] () {

knows

this

returns

an

integer

1; }

I specify the return value explicitly


//

now

int

we ' r e

[ ] ( ) >

return

telling

the

compiler

1; }

what

we

want

Example: Sorting a list


I We create a list of cities.

// D e f i n e a c l a s s f o r a c i t y
class City {
public :

int
int
int

};

int

population ;
foundation_year ;
surface_area ;

main ( ) {
s t d : : l i s t <C i t y > c i t i e s ;

// F i l l t h e l i s t w i t h d a t a
}

I To sort a set of objects, we have to dene a comparison


function.

Example: Sorting a list (cont.)


I In the STL, comparison functions have two arguments,
and return true if the rst argument goes before the
second.

I To sort by population, we can dene a compare function:

b o o l compare ( C i t y a , C i t y b ) {
a . population < b . population ;
}
...
c i t i e s . s o r t ( compare ) ;

return

I Alternatively, we can use a lambda function:

c i t i e s . s o r t ( [ ] ( C i t y a , C i t y b)> b o o l
{
a . pupulation < b . population ; } ) ;

return

Multithreading
I C++11 threads are more convenient than directly using
Posix threads.

#include <t h r e a d >


int main ( ) {
...

// D e c l a r i n g t h r e a d s
std : : thread

t;

// A s s i g n a f u n c t i o n
t = std : : thread ( function ,

arguments ) ;

// F i n a l i z i n g
t . join ();
...
}

I Compile with the ag

-pthread

Multithreading example: Dot product


// D e f i n i t i o n o f a c l a s s t o s t o r e t h e v a l u e s
c l a s s PartialDP {
public :
dotproduct ;
*x ;
*y ;
first ;
// F i r s t
l e n g t h ; // Count

double
double
double
int
int

index of array
of elements

PartialDP () : dotproduct (0){}


set (
* xin ,
* yin ,
x = xin ; y = yin ; f i r s t = f ;
}

double

operator

}
};

int

int

f ,
l ){
length = l ;

()

() {
i = f i r s t ; i < f i r s t + length ;
d o t p r o d u c t += x [ i ] + y [ i ] ;

for ( int

double

i ++) {

Multithreading example (cont.)

#include <t h r e a d >


const int s i z e = 1 0 0 0 , n t h r e a d s = 4 ;
int main ( ) {
std : : thread t [ nthreads ] ;
double x [ s i z e ] , y [ s i z e ] ;
int s u b s i z e = s i z e / n t h r e a d s ; // Assume
P a r t i a l D P dp [ n t h r e a d s ] ;
for ( int i = 0 ; i < n t h r e a d s ; i ++){
dp [ i ] . s e t ( x , y , i * s u b s i z e ,
t [ i ] = s t d : : t h r e a d ( dp [ i ] ) ;

no r e m a i n d e r

subsize );

// Compute

for ( int

i = 0;

i < nthreads ;

i ++) t [ i ] . j o i n ( ) ;

// Accumulate t h e p r o d u c t

double r e s u l t =
for ( int i = 0 ;

0;
i < n t h r e a d s ; i ++)
r e s u l t += dp [ i ] . d o t p r o d u c t ;

Compiling C++11

I With gcc, C++11 features can be compiled by using

-std=c++11 <args>

g++

Vous aimerez peut-être aussi