bigger better more incl. copyright · template < valuetype t, allocator alloc = allocator>...

47
Bigger Better More The new C++ Standard Library Thomas Witt April 24 2009 Copyright © 2009 Thomas Witt

Upload: others

Post on 19-Apr-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Bigger Better MoreThe new C++ Standard Library

Thomas WittApril 24 2009

Copyright © 2009 Thomas Witt

Page 2: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Landscape

• C99 (2003)

• Technical Report on C++ Library Extensions (TR1) Jan 2006

• Committee Draft (CD) Oct 2008

• C++ 0x

• TR2

• .NET, Java, Python, anybody?Copyright © 2009 Thomas Witt

Page 3: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

C++ 0x Goals

• Maintain stability and compatibility

• Prefer libraries to language extensions

• Prefer generality to specialization

• Support both experts and novices

• Increase type safety

• ...

Copyright © 2009 Thomas Witt

Page 4: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

C++ 0x Results

• Major core language features

• Few high profile library extensions

• Networking?

• File system access?

• XML?

• Library spec grew more than twofold

Copyright © 2009 Thomas Witt

Page 5: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Disclaimer

Churn

Copyright © 2009 Thomas Witt

Page 6: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Disclaimer

Copyright © 2009 Thomas Witt

Page 7: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Disclaimer

ChurnCopyright © 2009 Thomas Witt

Page 8: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Language Changes

• C++ 0x brings significant change to the core language

• New features make library writing and authoring easier

• Features were retrofitted to the Standard Library

Copyright © 2009 Thomas Witt

Page 9: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Rvalue References

• Move semantics

basic_string(basic_string&& str);

std::string failure = mogrify(std::string(“Brilliant idea”));

• Perfect forwarding

• Move helpers

Copyright © 2009 Thomas Witt

Page 10: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Concepts

• Constrained and unconstrained templates don’t mesh well

• Large parts of the standard library need to be conceptified

• Providing primitives

• Converting existing specification to code

Copyright © 2009 Thomas Witt

Page 11: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

auto concept MoveConstructible<typename T> : Constructible<T, T&&> { requires RvalueOf<T> && Constructible<T, RvalueOf<T>::type>;}

auto concept HasPlus<typename T, typename U> { typename result_type; result_type operator+(const T&, const U&);}

Copyright © 2009 Thomas Witt

Page 12: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

template < ValueType T, Allocator Alloc = allocator<T>>requires MoveConstructible<T>class vector;

template <InputIterator Iter>requires AllocatableElement< Alloc, T, Iter::reference> && MoveAssignable<T>void insert( const_iterator position , Iter first , Iter last);

Copyright © 2009 Thomas Witt

Page 13: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Variadic Templatestemplate< CopyConstructible F , CopyConstructible... BoundArgs>unspecified bind(F f, BoundArgs... bound_args);

template <class... Args> requires AllocatableElement< Alloc, T, Args&&...> && MoveAssignable<T>iterator emplace( const_iterator position , Args&&... args);

Copyright © 2009 Thomas Witt

Page 14: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Initializer Lists

requires AllocatableElement<Alloc, T, const T&>vector( initializer_list<T> , const Allocator& = Allocator());

std::vector<int> listOfInt = { 0, 17, 42, 7, 9, 39 };

Copyright © 2009 Thomas Witt

Page 15: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

There is more

• Thread support

• long long

• Constant expressions

• Deleted functions

• Explicit conversion operators

• nullptr_t

Copyright © 2009 Thomas Witt

Page 16: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

TR1 Revisited

• Bind, function, reference wrappers

• Smart Pointers

• Regular Expressions

• Random numbers

• Math special functions

Copyright © 2009 Thomas Witt

Page 17: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

TR1 Revisited

• Containers

• array

• tuple

• Unordered associative containers

• Type traits

• C99 library additions

Copyright © 2009 Thomas Witt

Page 18: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

TR1 Revisited

• C++ 0x incorporates TR1

• All of Gaul TR1?

• No - one little piece moved to its own document

• Extensions to the C++ Library to Support Mathematical Special Functions

Copyright © 2009 Thomas Witt

Page 19: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Smart Pointers

• Strict ownership

• unique_ptr

• Shared ownership

• shared_ptr

• Weak ownership

• weak_ptr

Copyright © 2009 Thomas Witt

Page 20: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

unique_ptr

• auto_ptr done right

• Moveable only

• No destructive copy

• Custom deleter

template < class T, class D = default_delete<T>>class unique_ptr;

Copyright © 2009 Thomas Witt

Page 21: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

shared_ptr extensions

• Allocators

• Aliasing

• Factory functions

• Atomic access

• Comparison

Copyright © 2009 Thomas Witt

Page 22: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

• 16 new algorithms

• Add obvious omissions

• Follow existing practice

• Useful additions

Algorithms

Copyright © 2009 Thomas Witt

Page 23: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

• copy_n

• uninitialized_copy_n

• all_of

• any_of

• none_of

• copy_if

Copyright © 2009 Thomas Witt

Page 24: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

• find_if_not

• partition_copy

• partition_point

• is_partitioned

• iota

• minmax_element

Copyright © 2009 Thomas Witt

Page 25: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

• is_sorted

• is_sorted_until

• is_heap

• is_heap_until

Copyright © 2009 Thomas Witt

Page 26: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

• const_iterator arguments in insert/erase

• cbegin(), cend()

• shrink_to_fit()

• data()

• vector<bool> has a spec!

Containers

Copyright © 2009 Thomas Witt

Page 27: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Emplace

• Placement insert

• Non-moveconstructible elements in containers

template <class... Args>requires AllocatableElement< Alloc, T, Args&&...>void emplace_back(Args&&... args);

Copyright © 2009 Thomas Witt

Page 28: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

forward_list

• Singly linked list

• On par with C implementation

• Insert after

template < class T, class Allocator = allocator<T> > class forward_list;

Copyright © 2009 Thomas Witt

Page 29: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Insert after?

• insert-, emplace-, splice-, erase_after

• No O(N) insert, erase, …

• begin_before()

Head NodeNode

Iterator

Copyright © 2009 Thomas Witt

Page 30: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Diagnostics

• System error support

• class system_error

• class error_code

• class error_category

• class error_condition

• Detailed error reporting from I/O streams

Copyright © 2009 Thomas Witt

Page 31: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Strings

• Uniform use of string

• Simple numeric access

• Unicode support

• No more Copy-On-Write

Copyright © 2009 Thomas Witt

Page 32: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

wstring_convert

wstring_convert<codecvt_utf8<wchar_t> myconv(); std::string mbstring = myconv.to_bytes(L"Hello\n");

Copyright © 2009 Thomas Witt

Page 33: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Exceptions

• Transporting exceptions between threads

• class exception_ptr

• current_exception()

• copy_exception(e)

• Nesting exception objects

Copyright © 2009 Thomas Witt

Page 34: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Allocators

Well ... No

Copyright © 2009 Thomas Witt

Page 35: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Copyright © 2009 Thomas Witt

Page 36: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Multithreading

Copyright © 2009 Thomas Witt

Page 37: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Atomics

• Memory ordering

• Atomic types

• Integral

• Address

• Generic

• Fences

Copyright © 2009 Thomas Witt

Page 38: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Threads

• Well thread

• Mutexes

• recursive_mutex

• timed_mutex

• Condition variables

• Locks and lockers

Copyright © 2009 Thomas Witt

Page 39: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

thread

• Unique ownership

• Creation

template <class F> explicit thread(F f);

template <class F, class ...Args> thread(F&& f, Args&&... args);

• Join

• Detach

Copyright © 2009 Thomas Witt

Page 40: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Futures

• unique_future

• shared_future

• promise

• packaged_task

Copyright © 2009 Thomas Witt

Page 41: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

packaged_taskint thgttg(){ return 42;}

std::packaged_task<int()> task(thgttg);std::unique_future<int> fi=task.get_future();

std::thread task(std::move(task));

// ...

fi.wait();Copyright © 2009 Thomas Witt

Page 42: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

One more thing ...

Copyright © 2009 Thomas Witt

Page 43: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Time

• duration

seconds, minutes, nanoseconds

• time_point

Epoch plus/minus duration

• Clock

system_clock, monotonic_clock

Copyright © 2009 Thomas Witt

Page 44: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Odds and ends

• numeric_limits

• lowest

• digits10, max_digits10

• prev(it), next(it)

• min(1, 2, 3, ...)

• aligned_storage

Copyright © 2009 Thomas Witt

Page 45: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Deprecated features

• auto_ptr

• Again: auto_ptr!

• iterator_traits, iterator, iterator tags

• Binders

Copyright © 2009 Thomas Witt

Page 46: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Conclusion

• C++ 0x focuses on foundations and facilities that require language support

• Standard Library is changed significantly

• TR 2 will focus on Standard Library extensions

• Filesystem

• Networking

Copyright © 2009 Thomas Witt

Page 47: Bigger Better More incl. Copyright · template < ValueType T, Allocator Alloc = allocator> requires MoveConstructible class vector; template

Copyright © 2009 Thomas Witt