You are on page 1of 54

What is Boost?

Boost Libraries by Examples

Conclusion

References

Boost - An overview
Advanced C++ Programming

Thomas Heller

July 11, 2011

1/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

What is Boost?

Boost Libraries by Examples

Conclusion

2/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Boost is ...

A collection of free, peer-reviewed, portable open-source C++


libraries

3/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Boost is ...

A collection of free, peer-reviewed, portable open-source C++


libraries

...one of the most highly regarded and expertly designed


C++ library projects in the world.  Herb Sutter and Andrei
Alexandrescu

3/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Boost is ...

A collection of free, peer-reviewed, portable open-source C++


libraries

...one of the most highly regarded and expertly designed


C++ library projects in the world.  Herb Sutter and Andrei
Alexandrescu

... torturing compilers since 1999

3/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Boost is ...

A collection of free, peer-reviewed, portable open-source C++


libraries

...one of the most highly regarded and expertly designed


C++ library projects in the world.  Herb Sutter and Andrei
Alexandrescu

... torturing compilers since 1999

Boost is not ...

Java for C++

3/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Libraries List

A total of 108 libraries/components (as of version 1.47.0)

4/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Libraries List

A total of 108 libraries/components (as of version 1.47.0)


Categories

String and text processing

Containers

Iterators

Algorithms

Function Objects and

Correctness and testing

Data structures

Image processing

Input/Output

higher-order programming

Inter-language support

Generic Programming

Memory

Template Metaprogramming

Parsing

Preprocessor

Programming Interfaces

Miscellaneous

Broken compiler workarounds


4/ 20

Metaprogramming

Concurrent Programming

Math and numerics

What is Boost?

Boost Libraries by Examples

Conclusion

References

Libraries List
accumulators
any
array
asio
assign
bimap
bind and mem_fn
call_traits
chrono
circular_buer
compatibility
compressed_pair
concept check
cong
conversion
crc
date_time
dynamic_bitset
exception
enable_if
lesystem
yweight
foreach
format
function
function_types
functional
functional/factory

functional/forward
functional/hash
fusion
gil
geometry
graph
icl
integer
interprocess
interval
intrusive
in_place_factory
typed_in_place_factory
io state savers
iostreams
iterators
lambda
lexical_cast
math
math/complex number algorithms
math/common_factor
math/octonion
math/quaternion
math/special_functions
math/statistical distributions
minmax
MPI
mpl

meta state machine


multi_array
multi_index
numeric/conversion
operators
optional
parameter
Phoenix
pointer container
polygon
pool
preprocessor
program_options
property map
property tree
proto
python
random
range
ratio
rational
ref
regex
result_of
scope_exit
serialization
signals
signals2

smart_ptr
statechart
static_assert
spirit
string_algo
swap
system
test
thread
timer
tokenizer
TR1
tribool
tuple
type_traits
typeof
uBLAS
units
unordered
utility
value_initialized
uuid
variant
wave
xpressive

4/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Libraries that made it into C++11

array

regex

bind

result_of

chrono

smart_ptr

enable_if

static_assert

function

thread

range

tuple

ratio

type_traits

5/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Libraries shown here:

type_traits
optional

and

mpl

6/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Motivation
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out )
{
while ( first != last )
{
* out = * first ;
++ out ; ++ first ;
}
return out ;
}
Is this generic code the optimal solution?

7/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Motivation
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out )
{
while ( first != last )
{
* out = * first ;
++ out ; ++ first ;
}
return out ;
}
Using memcpy could be even faster! If ...

7/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Motivation
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out )
{
while ( first != last )
{
* out = * first ;
++ out ; ++ first ;
}
return out ;
}
Using memcpy could be even faster! If ...
1. ... Iter1 and Iter2 are pointers
2. ... Iter1 and Iter2 point to the same type
3. ... the type pointed to by Iter1 has a trivial assignment
operator
7/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits

Metafunctions to introspect certain properties of a type

Return

true_type if the property is met, or false_type if not

Metafunctions to transform one type into another

Return the transformed type

8/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits

Metafunctions to introspect certain properties of a type

Return

true_type if the property is met, or false_type if not

Metafunctions to transform one type into another

Return the transformed type

What is a Metafunction?

A Metafunction is a class (template) with an inner type member.

template < bool c >


struct bool_
{
typedef bool_ <c > type ;
static const bool value = c;
};

struct true_type
: bool_ < true >
{};
struct false_type
: bool_ < false >
{};

8/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits

Metafunctions to introspect certain properties of a type

Return

true_type if the property is met, or false_type if not

Metafunctions to transform one type into another

Return the transformed type

What is a Metafunction?

A Metafunction is a class (template) with an inner type member.

template < bool c >


struct bool_
is a bool_<c>
{
typedef bool_ <c > type ;
static const bool value = c;
};

struct true_type
: bool_ < true >
{};
struct false_type
: bool_ < false >
{};

8/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_void


Requirements

Metafunction with one argument

Return

true_type if the argument is of type void

Return

false_type otherwise

template < typename T >


struct is_void
: false_type
{};

9/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_void


Requirements

Metafunction with one argument

Return

true_type if the argument is of type void

Return

false_type otherwise

template < typename T >


struct is_void
: false_type
{};
template <>
struct is_void<void>
: true_type
{};

9/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_void


Requirements

Metafunction with one argument

Return

true_type if the argument is of type void

Return

false_type otherwise

template < typename T >


struct is_void
: false_type
{};

Template specialization

template <>
struct is_void<void>
: true_type
{};

9/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_pointer


Requirements

Metafunction with one argument

Return

true_type if the argument is a pointer

Return

false_type otherwise

template < typename T >


struct is_pointer
: false_type
{};

10/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_pointer


Requirements

Metafunction with one argument

Return

true_type if the argument is a pointer

Return

false_type otherwise

template < typename T >


struct is_pointer
: false_type
{};
template < typename T >
struct is_pointer<T*>
: true_type
{};

10/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_pointer


Requirements

Metafunction with one argument

Return

true_type if the argument is a pointer

Return

false_type otherwise

template < typename T >


struct is_pointer
: false_type
{};

Partial template specialization

template < typename T >


struct is_pointer<T*>
: true_type
{};

10/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_pointer


Requirements

Metafunction with one argument

Return

true_type if the argument is a pointer

Return

false_type otherwise

template < typename T >


struct is_pointer
: false_type
{};

Partial template specialization

template < typename T >


struct is_pointer<T*>
: true_type
{};
Similar:

is_reference, is_const and is_volatile


10/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_same


Requirements

Metafunction with two arguments

Return

true_type if both arguments are the same type

Return

false_type if the types dier

template < typename T1 , typename T2 >


struct is_same
: false_type
{};

11/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_same


Requirements

Metafunction with two arguments

Return

true_type if both arguments are the same type

Return

false_type if the types dier

template < typename T1 , typename T2 >


struct is_same
: false_type
{};
tempalte < typename T >
struct is_same<T, T>
: true_type
{};

11/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Example: is_same


Requirements

Metafunction with two arguments

Return

true_type if both arguments are the same type

Return

false_type if the types dier

template < typename T1 , typename T2 >


struct is_same
Partial template specialization
: false_type
{};
tempalte < typename T >
struct is_same<T, T>
: true_type
{};

11/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - There is more

For a full list of type traits refer to:

http://www.boost.org/doc/libs/release/libs/type_
traits/doc/html/index.html

Some type traits rely on intrinsic compiler support

12/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - There is more

For a full list of type traits refer to:

http://www.boost.org/doc/libs/release/libs/type_
traits/doc/html/index.html

Some type traits rely on intrinsic compiler support


-

is_union
is_pod
has_trivial_constructor
has_trivial_copy
has_trivial_assign
has_trivial_destructor
has_nothrow_constructor
has_nothrow_copy
has_nothrow_assign
has_virtual_destructor

12/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - There is more

For a full list of type traits refer to:

http://www.boost.org/doc/libs/release/libs/type_
traits/doc/html/index.html

Some type traits rely on intrinsic compiler support


-

is_union
is_pod
has_trivial_constructor
has_trivial_copy
has_trivial_assign
has_trivial_destructor
has_nothrow_constructor
has_nothrow_copy
has_nothrow_assign
has_virtual_destructor

Customization through partial specialization


12/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out )
{
while ( first != last )
{
* out = * first ;
++ out ; ++ first ;
}
}

return out ;

13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out ) ;
To optimize it, we need ...
1. ... Iter1 and Iter2 are pointers
2. ... Iter1 and Iter2 point to the same type
3. ... the type pointed to by Iter1 has a trivial assignment
operator

13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator

// 1.:
is_pointer < Iter1 >
is_pointer < Iter2 >

13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator

// 2.:
typedef typename std :: iterator_traits < Iter1 >:: value_type
value_type1 ;
typedef typename std :: iterator_traits < Iter2 >:: value_type
value_type2 ;
is_same < value_type1 , value_type2 >

13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator

// 3.:
typedef typename std :: iterator_traits < Iter1 >:: value_type
value_type ;
has_trivial_assign < value_type >

13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator

// Combined , pseudocode
typedef typename std :: iterator_traits < Iter1 >:: value_type
value_type1 ;
typedef typename std :: iterator_traits < Iter2 >:: value_type
value_type2 ;
if ( is_pointer < Iter1 > && is_pointer < Iter2 >)
if ( is_same < value_type1 , value_type2 >)
if ( has_trivial_assign < value_type1 >)
return true_type ;
else
return false_type ;
else
return false_type ;
else
return false_type ;
13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator
// Combined :
template < typename Iter1 , typename Iter2 >
struct enable_memcpy
{
typedef typename std :: iterator_traits < Iter1 >:: value_type value_type1 ;
typedef typename std :: iterator_traits < Iter2 >:: value_type value_type2 ;
typedef typename
mpl::if_<
typename mpl::and_<
is_pointer < Iter1 >
, is_pointer < Iter2 >
>:: type
, typename mpl::if_<
typename is_same < value_type1 , value_type2 >:: type
, typename mpl::if_<
typename has_trivial_assign < value_type1 >:: type
, true_type
, false_type
>:: type
, false_type
>:: type
, false_type
>:: type
type ;
static const bool value = type :: value ;
13/ 20
};

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator
Boost.MPL Metafunctions!
// Combined :
template < typename Iter1 , typename Iter2 >
struct enable_memcpy
{
typedef typename std :: iterator_traits < Iter1 >:: value_type value_type1 ;
typedef typename std :: iterator_traits < Iter2 >:: value_type value_type2 ;
typedef typename
mpl::if_<
typename mpl::and_<
is_pointer < Iter1 >
, is_pointer < Iter2 >
>:: type
, typename mpl::if_<
typename is_same < value_type1 , value_type2 >:: type
, typename mpl::if_<
typename has_trivial_assign < value_type1 >:: type
, true_type
, false_type
>:: type
, false_type
>:: type
, false_type
>:: type
type ;
static const bool value = type :: value ;
13/ 20
};

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator
Boost.MPL Metafunction: mpl::if_:
// Combined :
template < typename Iter1 , typename Iter2 >
template < typename Cond , typename Then , typename Else >
struct enable_memcpy
struct if_ ;
{
typedef typename std :: iterator_traits < Iter1 >:: value_type value_type1 ;
template
< typename <Then
, >::
typename
Else >value_type2 ;
typedef typename std
:: iterator_traits
Iter2
value_type
struct if_ < true_type , Then , Else >
typedef typename
:
Then
mpl::if_<
{};
typename mpl::and_<
is_pointer < Iter1 >
template
, is_pointer
< Iter2 <>typename Then , typename Else >
struct if_ < false_type , Then , Else >
>:: type
, typename mpl::if_< : Else
{};is_same < value_type1 , value_type2 >:: type
typename
, typename mpl::if_<
typename has_trivial_assign < value_type1 >:: type
, true_type
, false_type
>:: type
, false_type
>:: type
, false_type
>:: type
type ;
static const bool value = type :: value ;
13/ 20
};

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator
Boost.MPL Metafunction: mpl::and_:
// Combined :
template < typename Iter1 , typename Iter2 >
template < typename Cond1 , typename Cond2 >
struct enable_memcpy
struct and_
{
: false_type
typedef typename std :: iterator_traits
< Iter1 >:: value_type value_type1 ;
{};
typedef typename std :: iterator_traits
< Iter2 >:: value_type value_type2 ;
typedef typename
template
<>
mpl::if_<
typename mpl::and_< struct and_ < true_type , true_type >
is_pointer < Iter1 > : true_type
{};>
, is_pointer < Iter2
>:: type
, typename mpl::if_<
typename is_same < value_type1 , value_type2 >:: type
, typename mpl::if_<
typename has_trivial_assign < value_type1 >:: type
, true_type
, false_type
>:: type
, false_type
>:: type
, false_type
>:: type
type ;
static const bool value = type :: value ;
13/ 20
};

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator
// Using it :
template < typename Iter1 , typename Iter2 >
struct enable_memcpy ;
namespace impl_detail {
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out , true_type )
{
memcpy ( out , first , last - first );
return out +( last - first );
}
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out , false_type )
{
while ( first != last )
{
* out = * first ;
++ out ; ++ first ;
}
return out ;
}
}
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out )
{
// ...
}

13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - Revisiting the motivational example


1. Iter1 and Iter2 are pointers
2. Iter1 and Iter2 point to the same type
3. the type pointed to by Iter1 has a trivial assignment operator
// Using it :
template < typename Iter1 , typename Iter2 >
struct enable_memcpy ;
namespace impl_detail {
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out , true_type )
{
// ...
}
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out , false_type )
{
// ...
}
}
template < typename Iter1 , typename Iter2 >
Iter2 copy ( Iter1 first , Iter1 last , Iter2 out )
{
return
impl_detail :: copy ( first , last , out
, typename enable_memcpy < Iter1 , Iter2 >:: type () );
}
13/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

type_traits - So ...

... why bother?

type_traits are used throughout the vast diversity of boost


libraries

Together with mpl, enable_if and result_of are the core


building blocks of boost

They make generic code more robust and costumizable

14/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

optional - Motivation
Consider these three functions:
1.

double sqrt(double n);

2.

char get_async_input();

3.

point polygon::get_any_point_inside();

15/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

optional - Motivation
Consider these three functions:
1.

double sqrt(double n);

2.

char get_async_input();

3.

point polygon::get_any_point_inside();

All of them could fail to return a valid value

15/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

optional - Motivation
Consider these three functions:
1.

double sqrt(double n);

2.

char get_async_input();

3.

point polygon::get_any_point_inside();

All of them could fail to return a valid value

Possible Solutions:

Assert

Exceptions

Some kind of special values

15/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

optional - Motivation
Consider these three functions:
1.

double sqrt(double n);

2.

char get_async_input();

3.

point polygon::get_any_point_inside();

All of them could fail to return a valid value

Possible Solutions:

Assert

Exceptions

Some kind of special values

Return

std::pair<T, bool>

15/ 20

What is Boost?

Boost Libraries by Examples

optional<T>

Conclusion

vs std::pair<T,

T can be anything

optional < point >


polygon :: get_any_poin_inside () ;
optional < point >
p = poly . get_any_point_inside () ;
if (p)
flood_fill (* p) ;

References

bool>

T must be
DefaultConstructable

std :: pair < point , bool >


polygon :: get_any_point_inside () ;
std :: pair < point , bool >
p = poly . get_any_point_inside () ;
if (p. second )
flood_fill (p. first );

16/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

When to use Boost?

Whenever you can!

17/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

When to use Boost?

Whenever you can!

When not to use Boost?

In environments that don't have the C++ stdlib

In environments with non-compliant compilers

17/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

When to use Boost?

Whenever you can!

When not to use Boost?

In environments that don't have the C++ stdlib

In environments with non-compliant compilers

Don't reinvent the wheel, look what Boost has to oer for you!

17/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Criticism against Boost

It's huge and monolothic!

Compile times are too long!

Compiler error messages are messy!

18/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

Questions?

19/ 20

What is Boost?

Boost Libraries by Examples

Conclusion

References

http://www.boost.org
http:

//boost.org/doc/libs/release/libs/libraries.htm

http://www.boost.org/doc/libs/release/libs/type_

traits/doc/html/index.html

http://www.boost.org/doc/libs/1_46_1/libs/mpl/doc/

index.html

http://www.boost.org/doc/libs/release/libs/

optional/doc/html/index.html

20/ 20

You might also like