78
1 2008 Pearson Education, Inc. All rights rese 2 4 Boost Libraries, Technical Report 1 and C++0x

2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

Embed Size (px)

Citation preview

Page 1: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

1

2008 Pearson Education, Inc. All rights reserved.

2424

Boost Libraries,Technical Report 1

and C++0x

Page 2: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

2

2008 Pearson Education, Inc. All rights reserved.

Outstanding leaders go out of their way toboost the self-esteem of their personnel.

— Sam Walton

Practice and thought mightgradually forge many an art.

— Virgil

I think “No comment” is a splendid expression.— Sir Winston Spencer Churchill

Page 3: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

3

2008 Pearson Education, Inc. All rights reserved.

So long as you are secure youwill count many friends.

— Ovid

The danger from computers is not thatthey will eventually get as smart as men,but we will meanwhile agree to meet them halfway.

— Bernard Avishai

Page 4: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

4

2008 Pearson Education, Inc. All rights reserved.

OBJECTIVES

In this chapter you will learn:

Future directions for C++.

What the Boost Libraries are.

A brief history of the Boost open source project, how new libraries are added to Boost, and howto install Boost.

To use Boost.Regex to search for strings,validate data and replace parts of stringsusing regular expressions.

Page 5: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

5

2008 Pearson Education, Inc. All rights reserved.

OBJECTIVES To avoid memory leaks by using Boost.Smart_ptr

to manage dynamic memory allocation and deallocation.

What Boost (and other) libraries are included in Technical Report 1 (TR1)—a description of the additions to the C++ Standard Library.

The changes to the core language and Standard Library coming in the new C++ Standard—C++0x.

To follow the Deitel online C++ Resource Centers for updates on the evolution of Boost, the Technical Reports and C++0x.

Page 6: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

6

2008 Pearson Education, Inc. All rights reserved.

24.1 Introduction

24.2 Deitel Online C++ and Related Resource Centers

24.3 Boost Libraries

24.4 Adding a New Library to Boost

24.5 Installing the Boost Libraries

24.6 Boost Libraries in Technical Report 1 (TR1)

24.7 Regular Expressions with the Boost.Regex Library

24.7.1 Regular Expression Example

24.7.2 Validating User Input with Regular Expressions

24.7.3 Replacing and Splitting Strings

Page 7: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

7

2008 Pearson Education, Inc. All rights reserved.

24.8 Smart Pointers with Boost.Smart_ptr

24.8.1 Reference Counted shared_ptr

24.8.2 weak_ptr: shared_ptr Observer

24.9 Technical Report

24.10 C++0x

24.11 Core Language Changes

24.12 Wrap-Up

Page 8: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

8

1992-2008 Pearson Education, Inc. All rights reserved.

24.1 Introduction

Page 9: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

9

1992-2008 Pearson Education, Inc. All rights reserved.

24.2 Deitel Online C++ and Related Resource Centers

• www.deitel.com/CPlusPlusBoostLibraries/

• www.deitel.com/cplusplus/

Page 10: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

10

1992-2008 Pearson Education, Inc. All rights reserved.

24.3 Boost Libraries

• www.boost.org

• 70+ free, open source C++ libraries

• Peer-reviewed

Page 11: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

11

1992-2008 Pearson Education, Inc. All rights reserved.

24.4 Adding a New Library to Boost

• Libraries must conform to the C++ standard

• Use only the C++ Standard Library and other Boost libraries

• Complete formal review process

• Conform to Boost Software License– Free for commercial and noncommercial use

Page 12: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

12

1992-2008 Pearson Education, Inc. All rights reserved.

24.5 Installing the Boost Libraries

• www.boost.org/more/getting_started/

• www.deitel.com/books/cpphtp6

Page 13: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

13

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1)

• TR1 is a description of proposed changes and additions to the C++ Standard Library

• Many of the libraries in TR1 originated in Boost

Page 14: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

14

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

• Boost.Array– Fixed-size arrays

– Support most of the STL container interface

– Use fixed-size arrays in STL applications

• Boost.Bind – Adapt functions that take up to nine arguments

– Reorder the arguments passed to the function using placeholders

Page 15: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

15

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

• Boost.Function– Store function pointers, member-function pointers

and function objects in a function wrapper

– Store a reference to a function object using the ref and cref functions added to the <utility> header

– Hold any function whose arguments and return type can be converted to match the signature of the function wrapper

Page 16: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

16

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

• Boost.mem_fn – Create a function object with a pointer, reference or

smart pointer to a member function

– The member function may take more than one argument

– mem_fn is a more flexible version of mem_fun and mem_fun_ref.

Page 17: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

17

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

• Boost.Random– Pseudo-random number generator uses an initial

state to produce seemingly random numbers—using the same initial state produces the same sequence of numbers

• Often used in testing

– Nondeterministic random number generator—a set of random numbers that can’t be predicted

• simulations and security scenarios where predictability is undesirable.

– Specify the distribution of the numbers generated

Page 18: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

18

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

• Boost.Ref– reference_wrapper object contains a reference and

allows an algorithm to use it as a value– Improves performance when passing large objects to an

algorithm• Boost.Regex

– Regular expressions– Match specific character patterns in text– Search for a particular expression in a string– Replace parts of a string that match a regular expression– Split a string into tokens using regular expressions to

define the delimiters– Text processing, parsing and input validation

Page 19: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

19

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

•result_of– Specify the return type of a call expression based on

the types of the arguments

– Helpful in templatizing the return types of functions and function objects

Page 20: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

20

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

• Boost.Smart_ptr– manage dynamically allocated resources– shared_ptrs handle lifetime management of

dynamically allocated objects– weak_ptrs allow you to observe the value held by a shared_ptr without assuming any management responsibilities

• Boost.Tuple – create sets of objects in a generic way and allows

generic functions to act on those sets– basically an extension to the STL’s std::pair

Page 21: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

21

1992-2008 Pearson Education, Inc. All rights reserved.

24.6 Boost Libraries in Technical Report 1 (TR1) (Cont.)

• Boost.Type_traits– determine specific traits of a type (e.g., is it a pointer

or a reference type, or does the type have a const qualifier?)

– perform type transformations to allow the object to be used in generic code

Page 22: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

22

1992-2008 Pearson Education, Inc. All rights reserved.

24.7 Regular Expressions with the Boost.Regex Library

• Regular expressions are specially formatted strings that are used to find patterns in text

• They can be used to validate data to ensure that it is in a particular format

Page 23: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

23

1992-2008 Pearson Education, Inc. All rights reserved.

24.7 Regular Expressions with the Boost.Regex Library (Cont.)

• The Boost.Regex library– Include the header file "boost/regex.hpp"

– Class template basic_regex represents a regular expression

– Algorithm regex_match returns true if a string matches the regular expression

• The entire string must match the regular expression

– Algorithm regex_search returns true if any part of an arbitrary string matches the regular expression

Page 24: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

24

1992-2008 Pearson Education, Inc. All rights reserved.

24.7 Regular Expressions with the Boost.Regex Library (Cont.)

• A character class is an escape sequence that represents a group of characters

– A word character (\w) is any alphanumeric character or underscore

– A whitespace character (\s) is a space, tab, carriage return, newline or form feed

– A digit (\d) is any numeric character

Page 25: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

25

2008 Pearson Education, Inc. All rights reserved.

Character class Matches

Character class Matches

\d any digit \D any non-digit

\w any word character \W any non-word character

\s any whitespace \S any non-whitespace

Fig. 24.1 | Character classes.

Page 26: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

26

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.2: RegexMatches.cpp 2 // Demonstrating regular expressions. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6

7 #include <string> 8 using std::string; 9

10 #include "boost/regex.hpp" 11

12 int main() 13 { 14 // create a regular expression 15 boost::regex expression( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" ); 16

17 // create a string to be tested 18 string string1 = "Jane's Birthday is 05-12-75\n" 19 "Dave's Birthday is 11-04-68\n" 20 "John's Birthday is 04-28-73\n" 21 "Joe's Birthday is 12-17-77";

Outline

RegexMatches.cpp

(1 of 2)Include regex.hpp header file

regex typedef represents a regular expression

string to be searched

Page 27: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

27

2008 Pearson Education, Inc. All rights reserved.

22

23 // create a boost::smatch object to hold the search results

24 boost::smatch match;

25

26 // match regular expression to string and print out all matches

27 while ( boost::regex_search( string1, match, expression,

28 boost::match_not_dot_newline ) )

29 {

30 cout << match << endl; // print the matching string

31

32 // remove the matched substring from the string

33 string1 = match.suffix();

34 } // end while

35

36 return 0;

37 } // end function main

Jane's Birthday is 05-12-75 Joe's Birthday is 12-17-77

Outline

RegexMatches.cpp

(2 of 2)

match_results object that stores matches as a string::const_iterator

Search the string until no matches can be found“.” character won’t match a

newline character

Return the original string starting at the end of the match

Page 28: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

28

2008 Pearson Education, Inc. All rights reserved.

Quantifier Matches

* Matches zero or more occurrences of the preceding pattern.

+ Matches one or more occurrences of the preceding pattern.

? Matches zero or one occurrences of the preceding pattern.

{n} Matches exactly n occurrences of the preceding pattern.

{n,} Matches at least n occurrences of the preceding pattern.

{n,m} Matches between n and m (inclusive) occurrences of the preceding pattern.

Fig. 24.3 | Quantifiers used in regular expressions.

Page 29: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

29

1992-2008 Pearson Education, Inc. All rights reserved.

24.7.1  Regular Expression Example

• You must escape the backslash character with another backslash, the character class \d must be represented as \\d in a C++ string literal

• The dot character "." matches any character

• Characters can be listed in square brackets, []– The pattern "[aeiou]" matches any vowel

– Ranges of characters are represented by placing a dash (-) between two characters

– ^ specifies that a pattern should match anything other than the characters in the brackets

Page 30: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

30

1992-2008 Pearson Education, Inc. All rights reserved.

24.7.1  Regular Expression Example (Cont.)

• Quantifiers are greedy—they match as many occurrences of the pattern as possible

• If a quantifier is followed by a question mark (?), the quantifier becomes lazy and will match as few occurrences as possible as long as there is a successful match.

Page 31: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

31

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.4: Validate.cpp

2 // Validating user input with regular expressions.

3 #include <iostream>

4 using std::cin;

5 using std::cout;

6 using std::endl;

7

8 #include <string>

9 using std::string;

10

11 #include "boost/regex.hpp"

12

13 bool validate( string, string ); // validate prototype

14 string inputData( string, string ); // inputData prototype

15

16 int main()

17 {

18 // enter the last name

19 string lastName = inputData( "last name", "[A-Z][a-zA-Z]*" );

20

21 // enter the first name

22 string firstName = inputData( "first name", "[A-Z][a-zA-Z]*" );

23

24 // enter the address

25 string address = inputData( "address",

26 "[0-9]+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );

27

Outline

Validate.cpp

(1 of 4)

Get data from the user

Page 32: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

32

2008 Pearson Education, Inc. All rights reserved.

28 // enter the city

29 string city =

30 inputData( "city", "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );

31

32 // enter the state

33 string state = inputData( "state",

34 "([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)" );

35

36 // enter the zip code

37 string zipCode = inputData( "zip code", "\\d{5}" );

38

39 // enter the phone number

40 string phoneNumber = inputData( "phone number",

41 "[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}" );

42

43 // display the validated data

44 cout << "\nValidated Data\n\n"

45 << "Last name: " << lastName << endl

46 << "First name: " << firstName << endl

47 << "Address: " << address << endl

48 << "City: " << city << endl

49 << "State: " << state << endl

50 << "Zip code: " << zipCode << endl

51 << "Phone number: " << phoneNumber << endl;

52

53 return 0;

54 } // end of function main

55

Outline

Validate.cpp

(2 of 4)

“|” matches the expression on its left or its right

Display the validated data

Page 33: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

33

2008 Pearson Education, Inc. All rights reserved.

56 // validate the data format using a regular expression

57 bool validate( string data, string expression )

58 {

59 // create a regex to validate the data

60 boost::regex validationExpression = boost::regex( expression );

61 return boost::regex_match( data, validationExpression );

62 } // end of function validate

63

64 // collect input from the user

65 string inputData( string fieldName, string expression )

66 {

67 string data; // store the data collected

68

69 // request the data from the user

70 cout << "Enter " << fieldName << ": ";

71 getline( cin, data );

72

73 // validate the data

74 while ( !( validate( data, expression ) ) )

75 {

76 cout << "Invalid " << fieldName << ".\n";

77 cout << "Enter " << fieldName << ": ";

78 getline( cin, data );

79 } // end while

Outline

Validate.cpp

(3 of 4)

Return true if the entire string matches the regular expression

Collect input until the data matches the regular expression

Page 34: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

34

2008 Pearson Education, Inc. All rights reserved.

80

81 return data;

82 } // end of function inputData

Enter last name: Doe Enter first name: John Enter address: 123 Some Street Enter city: Some City Enter state: Some State Enter zip code: 12345 Enter phone number: 123-456-7890 Validated Data Last name: Doe First name: John Address: 123 Some Street City: Some City State: Some State Zip code: 12345 Phone number: 123-456-7890

Outline

Validate.cpp

(4 of 4)

Page 35: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

35

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.5: RegexSubstitution.cpp

2 // Using regex_replace algorithm.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 #include <string>

8 using std::string;

9

10 #include "boost/regex.hpp"

11

12 int main()

13 {

14 // create the test strings

15 string testString1 = "This sentence ends in 5 stars *****";

16 string testString2 = "1, 2, 3, 4, 5, 6, 7, 8";

17 string output;

18

19 cout << "Original string: " << testString1 << endl;

20

21 // replace every * with a ^

22 testString1 =

23 boost::regex_replace( testString1, boost::regex( "\\*" ), "^" );

24 cout << "^ substituted for *: " << testString1 << endl;

25

Outline

RegexSubstitution.cpp

(1 of 3)

strings to manipulate

Replace all occurrences of * with ^Escape the * to search for the literal character “*”

Page 36: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

36

2008 Pearson Education, Inc. All rights reserved.

26 // replace "stars" with "carets"

27 testString1 = boost::regex_replace(

28 testString1, boost::regex( "stars" ), "carets" );

29 cout << "\"carets\" substituted for \"stars\": "

30 << testString1 << endl;

31

32 // replace every word with "word"

33 testString1 = boost::regex_replace(

34 testString1, boost::regex( "\\w+" ), "word" );

35 cout << "Every word replaced by \"word\": " << testString1 << endl;

36

37 // replace the first three digits with "digit"

38 cout << "\nOriginal string: " << testString2 << endl;

39 string testString2Copy = testString2;

40

41 for ( int i = 0; i < 3; i++ ) // loop three times

42 {

43 testString2Copy = boost::regex_replace( testString2Copy,

44 boost::regex( "\\d" ), "digit", boost::format_first_only );

45 } // end for

46

47 cout << "Replace first 3 digits by \"digit\": "

48 << testString2Copy << endl;

49

50 // split the string at the commas

51 cout << "string split at commas [";

52

Outline

RegexSubstitution.cpp

(2 of 3)

Replace the word “stars” with “carets”

Replace every word (one or more word characters)

Replace only the first match

Page 37: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

37

2008 Pearson Education, Inc. All rights reserved.

53 boost::sregex_token_iterator tokenIterator( testString2.begin(),

54 testString2.end(), boost::regex( ",\\s" ), -1 ); // token iterator

55 boost::sregex_token_iterator end; // empty iterator

56

57 while ( tokenIterator != end ) // tokenIterator isn’t empty

58 {

59 output += "\"" + *tokenIterator + "\", "; // add the token to output

60 tokenIterator++; // advance the iterator

61 } // end while

62

63 // delete the ", " at the end of output string

64 cout << output.substr( 0, output.length() - 2 ) << "]" << endl;

65

66 return 0;

67 } // end of function main

Original string: This sentence ends in 5 stars ***** ^ substituted for *: This sentence ends in 5 stars ^^^^^ "carets" substituted for "stars": This sentence ends in 5 carets ^^^^^ Every word replaced by "word": word word word word word word ^^^^^ Original string: 1, 2, 3, 4, 5, 6, 7, 8 Replace first 3 digits by "digit": digit, digit, digit, 4, 5, 6, 7, 8 string split at commas ["1", "2", "3", "4", "5", "6", "7", "8"]

Outline

RegexSubstitution.cpp

(3 of 3)

sregex_token_iterator typedef manipulates tokens as string::const_iterator

Iterate over substrings that don’t match the regular expression

Page 38: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

38

1992-2008 Pearson Education, Inc. All rights reserved.

24.8  Smart Pointer with Boost.Smart_ptr

• Smart pointers strengthens the process of dynamic memory allocation and deallocation

• Smart pointers help you write exception safe code

•auto_ptr manages dynamically allocated memory– When copied, ownership of the memory is transferred

and the original is set to NULL

– Deletes the memory when destroyed

– Can’t point to an array

– Can’t be used with the STL containers

• TR1 includes shared_ptr and weak_ptr

Page 39: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

39

1992-2008 Pearson Education, Inc. All rights reserved.

24.8.1  Reference Counted shared_ptr

•shared_ptrs hold an internal pointer to a resource (e.g., a dynamically allocated object) that may be shared with other objects in the program

• You can have any number of shared_ptrs to the same resource

• If you change the resource with one shared_ptr, the changes will be “seen” by the other shared_ptrs

Page 40: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

40

1992-2008 Pearson Education, Inc. All rights reserved.

24.8.1  Reference Counted shared_ptr (Cont.)

• The internal pointer is deleted once the last shared_ptr to the resource is destroyed

•shared_ptrs use reference counting to determine how many shared_ptrs point to the resource

•shared_ptrs can safely be copied and used in STL containers

• A custom deleter function, or function object, can be used to destroy the resource

Page 41: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

41

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.6: Book.h

2 // Declaration of class Book.

3 #ifndef BOOK_H

4 #define BOOK_H

5 #include <string>

6 using std::string;

7

8 class Book

9 {

10 public:

11 Book(const string &bookTitle ); // constructor

12 ~Book(); // destructor

13 string title; // title of the Book

14 };

15 #endif // BOOK_H

Outline

Book.h

Page 42: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

42

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.7: Book.cpp

2 // Member function definitions for class Book.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 #include <string>

8 using std::string;

9

10 #include "Book.h"

11

12 Book::Book( const string &bookTitle ) : title( bookTitle )

13 {

14 }

15

16 Book::~Book()

17 {

18 cout << "Destroying Book: " << title << endl;

19 } // end of destructor

Outline

Book.cpp

Indicate that an instance of class Book is being destroyed

Page 43: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

43

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.8: fig24_8.cpp

2 // Demonstrate use of shared_ptrs.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 #include <vector>

8 using std::vector;

9

10 #include "Book.h"

11 #include "boost/shared_ptr.hpp"

12

13 typedef boost::shared_ptr< Book > BookPtr; // shared_ptr to a Book

14

15 // a custom delete function for a pointer to a Book

16 void deleteBook( Book* book )

17 {

18 cout << "Custom deleter for a Book, ";

19 delete book; // delete the Book pointer

20 } // end of deleteBook

21

Outline

fig24_8.cpp

(1 of 4)typedef for a shared_ptr to a Book

Custom deleter function

Display a message and delete the pointer

Page 44: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

44

2008 Pearson Education, Inc. All rights reserved.

22 // compare the titles of two Books for sorting

23 bool compareTitles( BookPtr bookPtr1, BookPtr bookPtr2 )

24 {

25 return ( bookPtr1->title < bookPtr2->title );

26 } // end of compareTitles

27

28 int main()

29 {

30 // create a shared_ptr to a Book and display the reference count

31 BookPtr bookPtr( new Book( "C++ How to Program" ) );

32 cout << "Reference count for Book " << bookPtr->title << " is: "

33 << bookPtr.use_count() << endl;

34

35 // create another shared_ptr to the Book and display reference count

36 BookPtr bookPtr2( bookPtr );

37 cout << "Reference count for Book " << bookPtr->title << " is: "

38 << bookPtr.use_count() << endl;

39

40 // change the Book’s title and access it from both pointers

41 bookPtr2->title = "Java How to Program";

42 cout << "The Book's title changed for both pointers: "

43 << "\nbookPtr: " << bookPtr->title

44 << "\nbookPtr2: " << bookPtr2->title <<endl;

45

Outline

fig24_8.cpp

(2 of 4)

Compare the titles alphabeticallyCreate a shared_ptr to a pointer returned by new

Get the reference count for the shared_ptr

Page 45: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

45

2008 Pearson Education, Inc. All rights reserved.

46 // create a std::vector of shared_ptrs to Books (BookPtrs)

47 vector< BookPtr > books;

48 books.push_back( BookPtr( new Book( "C How to Program" ) ) );

49 books.push_back( BookPtr( new Book( "VB How to Program" ) ) );

50 books.push_back( BookPtr( new Book( "C# How to Program" ) ) );

51 books.push_back( BookPtr( new Book( "C++ How to Program" ) ) );

52

53 // print the Books in the vector

54 cout << "\nBooks before sorting: " << endl;

55 for ( int i = 0; i < books.size(); i++ )

56 cout << ( books[ i ] )->title << "\n";

57

58 // sort the vector by Book title and print the sorted vector

59 sort( books.begin(), books.end(), compareTitles );

60 cout << "\nBooks after sorting: " << endl;

61 for ( int i = 0; i < books.size(); i++ )

62 cout << ( books[ i ] )->title << "\n";

63

64 // create a shared_ptr with a custom deleter

65 cout << "\nshared_ptr with a custom deleter." << endl;

66 BookPtr bookPtr3( new Book( "Small C++ How to Program" ), deleteBook);

67 bookPtr3.reset(); // release the Book this shared_ptr manages

68

69 // shared_ptrs are going out of scope

70 cout << "\nAll shared_ptr objects are going out of scope." << endl;

71

72 return 0;

73 } // end of main

Outline

fig24_8.cpp

(3 of 4)

Add shared_ptrs to a vector

Sort the Books by titleCreate a shared_ptr with a

custom deleter

Release the shared_ptr’s resource

Page 46: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

46

2008 Pearson Education, Inc. All rights reserved.

Reference count for Book C++ How to Program is: 1 Reference count for Book C++ How to Program is: 2 The Book's title changed for both pointers: bookPtr: Java How to Program bookPtr2: Java How to Program Books before sorting: C How to Program VB How to Program C# How to Program C++ How to Program Books after sorting: C How to Program C# How to Program C++ How to Program VB How to Program shared_ptr with a custom deleter. Custom deleter for a Book, Destroying Book: Small C++ How to Program All shared_ptr objects are going out of scope. Destroying Book: C How to Program Destroying Book: C# How to Program Destroying Book: C++ How to Program Destroying Book: VB How to Program Destroying Book: Java How to Program

Outline

fig24_8.cpp

(4 of 4)

Page 47: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

47

1992-2008 Pearson Education, Inc. All rights reserved.

24.8.2  weak_ptr: shared_ptr Observer

•weak_ptr points to a resource managed by a shared_ptr without assuming any responsibility for it

•shared_ptr reference count doesn’t increase when a weak_ptr references it

• When the last shared_ptr is destroyed, the resource is deleted and any remaining weak_ptrs are set to NULL

• Avoid memory leaks caused by circular references.

Page 48: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

48

1992-2008 Pearson Education, Inc. All rights reserved.

24.8.2  weak_ptr: shared_ptr Observer (Cont.)

• A weak_ptr can’t directly access the resource it points to—you must create a shared_ptr from the weak_ptr to access the resource

– Pass the weak_ptr to the shared_ptr constructor

– If the resource has already been deleted, the shared_ptr constructor will throw a boost::bad_weak_ptr exception

– weak_ptr member function lock returns a shared_ptr to the weak_ptr’s resource

– If the weak_ptr points to NULL, lock will return an empty shared_ptr (i.e., a shared_ptr to NULL)

Page 49: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

49

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.9: Author.h

2 // Definition of class Author.

3 #ifndef AUTHOR_H

4 #define AUTHOR_H

5 #include <string>

6 using std::string;

7

8 #include "boost/shared_ptr.hpp"

9 #include "boost/weak_ptr.hpp"

10

11 class Book; // forward declaration of class Book

12

13 // Author class definition

14 class Author

15 {

16 public:

17 Author( const string &authorName ); // constructor

18 ~Author(); // destructor

19 void printBookTitle(); // print the title of the Book

20 string name; // name of the Author

21 boost::weak_ptr< Book > weakBookPtr; // Book the Author wrote

22 boost::shared_ptr< Book > sharedBookPtr; // Book the Author wrote

23 };

24 #endif // AUTHOR_H

Outline

Author.h

A weak_ptr to a Book

A shared_ptr to a Book

Page 50: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

50

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.10: Book.h

2 // Definition of class Book.

3 #ifndef BOOK_H

4 #define BOOK_H

5 #include <string>

6 using std::string;

7

8 #include "boost/shared_ptr.hpp"

9 #include "boost/weak_ptr.hpp"

10

11 class Author; // forward declaration of class Author

12

13 // Book class definition

14 class Book

15 {

16 public:

17 Book( const string &bookTitle ); // constructor

18 ~Book(); // destructor

19 void printAuthorName(); // print the name of the Author

20 string title; // title of the Book

21 boost::weak_ptr< Author > weakAuthorPtr; // Author of the Book

22 boost::shared_ptr< Author > sharedAuthorPtr; // Author of the Book

23 };

24 #endif // BOOK_H

Outline

Book.h

A weak_ptr to an Author

A shared_ptr to an Author

Page 51: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

51

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.11: Author.cpp

2 // Member function definitions for class Author.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 #include <string>

8 using std::string;

9

10 #include "Author.h"

11 #include "Book.h"

12 #include "boost/shared_ptr.hpp"

13 #include "boost/weak_ptr.hpp"

14

15 Author::Author( const string &authorName ) : name( authorName )

16 {

17 }

18

Outline

Author.cpp

(1 of 2)

Page 52: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

52

2008 Pearson Education, Inc. All rights reserved.

19 Author::~Author()

20 {

21 cout << "Destroying Author: " << name << endl;

22 } // end of destructor

23

24 // print the title of the Book this Author wrote

25 void Author::printBookTitle()

26 {

27 // if weakBookPtr.lock() returns a non-empty shared_ptr

28 if ( boost::shared_ptr< Book > bookPtr = weakBookPtr.lock() )

29 {

30 // show the reference count increase and print the Book's title

31 cout << "Reference count for Book " << bookPtr->title

32 << " is " << bookPtr.use_count() << "." << endl;

33 cout << "Author " << name << " wrote the book " << bookPtr->title

34 << "\n" << endl;

35 } // end if

36 else // weakBookPtr points to NULL

37 cout << "This Author has no Book." << endl;

38 } // end of printBookTitle

Outline

Author.cpp

(2 of 2)

Indicate when an instance of Author is being destroyed

Create a shared_ptr from the weak_ptr data member to access the resource

Page 53: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

53

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.12: Book.cpp

2 // Member function definitions for class Book.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 #include <string>

8 using std::string;

9

10 #include "Author.h"

11 #include "Book.h"

12 #include "boost/shared_ptr.hpp"

13 #include "boost/weak_ptr.hpp"

14

15 Book::Book( const string &bookTitle ) : title( bookTitle )

16 {

17 }

18

Outline

Book.cpp

(1 of 2)

Page 54: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

54

2008 Pearson Education, Inc. All rights reserved.

19 Book::~Book()

20 {

21 cout << "Destroying Book: " << title << endl;

22 } // end of destructor

23

24 // print the name of this Book's Author

25 void Book::printAuthorName()

26 {

27 // if weakAuthorPtr.lock() returns a non-empty shared_ptr

28 if ( boost::shared_ptr< Author > authorPtr = weakAuthorPtr.lock() )

29 {

30 // show the reference count increase and print the Author's name

31 cout << "Reference count for Author " << authorPtr->name

32 << " is " << authorPtr.use_count() << "." << endl;

33 cout << "The book " << title << " was written by "

34 << authorPtr->name << "\n" << endl;

35 } // end if

36 else // weakAuthorPtr points to NULL

37 cout << "This Book has no Author." << endl;

38 } // end of printAuthorName

Outline

Book.cpp

(2 of 2)

Indicate when an instance of Book is being destroyed

Create a shared_ptr from the weak_ptr data member to access the resource

Page 55: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

55

2008 Pearson Education, Inc. All rights reserved.

1 // Fig. 24.13: fig24_13.cpp

2 // Demonstrate use of weak_ptr.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 #include "Author.h"

8 #include "Book.h"

9 #include "boost/shared_ptr.hpp"

10

11 int main()

12 {

13 // create a Book and an Author

14 boost::shared_ptr< Book > bookPtr( new Book( "C++ How to Program" ) );

15 boost::shared_ptr< Author > authorPtr(

16 new Author( "Deitel & Deitel" ) );

17

18 // reference the Book and Author to each other

19 bookPtr->weakAuthorPtr = authorPtr;

20 authorPtr->weakBookPtr = bookPtr;

21

Outline

fig24_13.cpp

(1 of 3)

Create a circular reference between Book and Author using the weak_ptr data members

Page 56: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

56

2008 Pearson Education, Inc. All rights reserved.

22 // set the shared_ptr data members to create the memory leak

23 bookPtr->sharedAuthorPtr = authorPtr;

24 authorPtr->sharedBookPtr = bookPtr;

25

26 // reference count for bookPtr and authorPtr is one

27 cout << "Reference count for Book " << bookPtr->title << " is "

28 << bookPtr.use_count() << endl;

29 cout << "Reference count for Author " << authorPtr->name << " is "

30 << authorPtr.use_count() << "\n" << endl; 31 32 // access the cross references to print the data they point to

33 cout << "\nAccess the Author's name and the Book's title through "

34 << "weak_ptrs." << endl;

35 bookPtr->printAuthorName();

36 authorPtr->printBookTitle();

37

38 // reference count for each shared_ptr is back to one

39 cout << "Reference count for Book " << bookPtr->title << " is "

40 << bookPtr.use_count() << endl;

41 cout << "Reference count for Author " << authorPtr->name << " is "

42 << authorPtr.use_count() << "\n" << endl;

43

Outline

fig24_13.cpp

(2 of 3)

Create a circular reference between Book and Author using the shared_ptr data members

Print the Book’s Author

Print the Author’s Book

Page 57: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

57

2008 Pearson Education, Inc. All rights reserved.

44 // the shared_ptrs go out of scope, the Book and Author are destroyed

45 cout << "The shared_ptrs are going out of scope." << endl;

46

47 return 0;

48 } // end of main

Reference count for Book C++ How to Program is 2 Reference count for Author Deitel & Deitel is 2 Access the Author's name and the Book's title through weak_ptrs. Reference count for Author Deitel & Deitel is 3. The book C++ How to Program was written by Deitel & Deitel Reference count for Book C++ How to Program is 3. Author Deitel & Deitel wrote the book C++ How to Program Reference count for Book C++ How to Program is 2 Reference count for Author Deitel & Deitel is 2 The shared_ptrs are going out of scope.

Outline

fig24_13.cpp

(3 of 3)

The instances of Book and Author aren’t destroyed, there is a memory leak

Page 58: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

58

2008 Pearson Education, Inc. All rights reserved.

Reference count for Book C++ How to Program is 1

Reference count for Author Deitel & Deitel is 1

Access the Author's name and the Book's title through weak_ptrs.

Reference count for Author Deitel & Deitel is 2.

The book C++ How to Program was written by Deitel & Deitel

Reference count for Book C++ How to Program is 2.

Author Deitel & Deitel wrote the book C++ How to Program

Reference count for Book C++ How to Program is 1

Reference count for Author Deitel & Deitel is 1

The shared_ptrs are going out of scope.

Destroying Author: Deitel & Deitel

Destroying Book: C++ How to Program

Outline

Fix the memory leak by not setting the shared_ptr data members

Page 59: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

59

1992-2008 Pearson Education, Inc. All rights reserved.

24.9  Technical Report 1

• Technical Report 1 (TR1) describes proposed additions to the C++ Standard Library

• Many of the libraries in TR1 will be accepted by the C++ Standards Committee but they are not considered part of the C++ standard until the next version is finalized

• Technical Report 2 (TR2), which will be released shortly after C++0x, contains additional library proposals that weren’t included in TR1

Page 60: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

60

1992-2008 Pearson Education, Inc. All rights reserved.

24.9  Technical Report 1 (Cont.)

• The Unordered Associative Containers library

– unordered_set

– unordered_map

– unordered_multiset

– unordered_multimap

– Implemented as hash tables

– Defined in the <unordered_set> and <undordered_map> headers.

Page 61: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

61

1992-2008 Pearson Education, Inc. All rights reserved.

24.9  Technical Report 1 (Cont.)

– A key is used to determine where to store an element in the container

– Retrieve an element using the key much as you do with a set or map

– unordered_set and unordered_multiset, the element itself is used as the key

– unordered_map and unordered_multimap use a separate key, elements passed as a pair< const Key, Value >

– unordered_set and unordered_map require that all the keys are unique; unordered_multiset and unordered_multimap don’t

Page 62: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

62

1992-2008 Pearson Education, Inc. All rights reserved.

24.9  Technical Report 1 (Cont.)

• Mathematical Special Functions– This library incorporates mathematical functions

added to C99 that are missing in the C++ Standard

– C99 supplies trigonometric, hyperbolic, exponential, logarithmic, power and special functions

– <cmath> header.

• Increased Compatibility with C99– Increase compatibility between C++ and C99—often

by including corresponding C99 headers.

Page 63: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

63

1992-2008 Pearson Education, Inc. All rights reserved.

24.10  C++0x

• The new standard, currently referred to as C++0x, is likely to be released in 2009

• Includes the TR1 libraries and changes to the core language

Page 64: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

64

1992-2008 Pearson Education, Inc. All rights reserved.

24.10  C++0x (Cont.)

• Standardization Process– The International Organization for Standardization

(ISO) oversees the creation of international programming language standards

– ISO/IEC JTC 1/SC 22 Working Group 21 (WG21) maintains the C++ standard

– Meets twice a year to discuss issues pertaining to the standard

– ISO requires at least 5 years between new drafts of a standard.

Page 65: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

65

1992-2008 Pearson Education, Inc. All rights reserved.

24.10  C++0x (Cont.)

• Goals for C++0x– Make C++ easier to learn

– Improve library building capabilities

– Increase compatibility with the C programming language

Page 66: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

66

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes

• A listing of all the proposed changes to the core language can be found at www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2228.html

• The GNU C++ compiler has an optional C++0x mode which allows you to experiment with a number of the core language changes (gcc.gnu.org/gcc-4.3/cxx0x_status.html).

Page 67: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

67

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Rvalue Reference – Rvalue reference allows you to bind an rvalue

(temporary object) to a non-const reference

– Declared as T&& (where T is the type of the object being referenced)

– Effectively implement move semantics

– “Forwarding functions”—function objects that adapt a function to take fewer arguments

Page 68: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

68

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

•static_assert– Test certain aspects of the program at compile time– A static_assert declaration takes a constant

integral expression and a string literal. If the expression evaluates to 0 (false), the compiler reports the error, including the string literal provided in the declaration

– Can be used at namespace, class or block scope.– Provide more informative error messages– Report incorrect library usage much more

effectively

Page 69: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

69

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

•extern template– extern indicates that a variable or function is

defined either later in the current file or in a separate file

– extern template provides the same functionality for templates

• Extended friend Declarations– Clarifies the wording with respect to friend

declarations and templates.

Page 70: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

70

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Synchronizing the C++ Preprocessor with C99

– Create a C++ compiler and preprocessor with the same rules and features as the C99 compiler and preprocessor

– The changes affect areas including:• predefined macros

• the pragma operator

• string concatenation

• header and include filenames

Page 71: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

71

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Conditionally Supported Behavior– Any optional feature that is not supported by the

current implementation causes the compiler to return a report that the program uses an unsupported feature

– Helps novice programmers who may inadvertently use nonstandard features

Page 72: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

72

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Changing Undefined Behavior into Diagnosable Errors

– Changes certain situations which cause undefined behavior into diagnosable error

• oversized integers

• unsupported character escapes

• passing non-POD objects (“plain old data”) to variadic functions

Page 73: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

73

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Adding the long long Type to C++ – long long integer type that must be at least 64 bits

(8 bytes)

– Improves compatibility with the C99 Standard

• Adding Extended Integer Types to C++– An integer type defined by an implementation in

addition to the integer types required by the C++ Standard

– Any signed extended integer type must also have a corresponding unsigned type of the same size

– Explicitly using an extended integer type affects the portability of your program.

Page 74: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

74

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Delegating Constructors– A constructor can call another of the class’s constructors

– Easier to write overloaded constructors, common code doesn’t need to be repeated

• Right Angle Brackets– Without the space between trailing right angle brackets,

the compiler assumes it’s the right shift operator (>>)

– Writing vector<class<T>> causes a compiler error

– Change the C++ compiler to recognize when >> is part of a template rather than the right-shift operator.

Page 75: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

75

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Deducing the Type of Variable from Its Initializer– auto keyword determines variable types based on the

initializer expression

– Use in place of long, complicated types

– Can be used with const and volatile qualifiers

– Can create pointers and references as you would with the full type name

– Supports the declaration of multiple variables in one statement (e.g., auto x = 1, y = 2)

– Save time, ease the learning process and improve generic programming

Page 76: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

76

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Variadic Templates– A variadic template accepts any number of arguments– Express classes and functions that take an arbitrary

number of arguments– Template parameters are put in a template type

parameter pack– Arguments passed to a function are put in a function

parameter pack– An ellipsis (“...”) to the left of the name of a template or

function parameter declares it as a template type parameter pack or function parameter pack, respectively

– An ellipsis to the right of a function parameter extracts all the elements of the parameter pack

– The nth element of a template type parameter pack is the type of the nth element in a function parameter pack

Page 77: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

77

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Template Aliases– A template alias introduces a name used to refer to a

template– Certain parameters are specified and others may

still vary

• New Character Types– Unicode character support– Already supported by the C standard– char16_t and char32_t represent the 16-bit and

32-bit Unicode characters– Prefixes u and U denote char16_t or char32_t

characters, respectively.

Page 78: 2008 Pearson Education, Inc. All rights reserved. 1 24 Boost Libraries, Technical Report 1 and C++0x

78

1992-2008 Pearson Education, Inc. All rights reserved.

24.11  Core Language Changes (Cont.)

• Extending sizeof– sizeof operator returns the number of bytes used

to store an object– Can be called on a data member without accessing it

through an instance of the class– Used more naturally with static data members.

• Alternative to Sequence Points– Changes the wording in the standard to define

sequencing requirements more precisely– Any attempt to add concurrency (i.e.,

multithreading) requires a solid understanding of the order of events in a program