220 likes | 248 Views
Learn about the improvements and new features in C++-0x, including Concepts, Memory Model, Concurrency, and more. Understand how to write efficient multi-threaded code and use variadic templates in your C++ programs. Stay updated with the latest language advancements.
E N D
C++-0x: The New C++ Standard Jonathan Caves Principal Software Engineer Visual C++ Microsoft Corporation
What is C++-0x? Revision to the existing C++ Standard - C++-03 Goals: to improve and simplify C++ Has been the focus of the C++ Committee since ~2004
Major Features Concepts Memory Model/Concurrency Garbage Collection Other features
Concepts • #include <list>#include <algorithm>extern void fill(std::list<int>&);int main(){ std::list<int> data; fill(data); std::sort(data.begin(), data.end());} • The problem:
Concepts Containers Algorithms Iterators
Concepts • The problem: • std::sort requires random access iterators • std::list doesn’t support random access iterators • Question: how can we make this information available to the compiler? • Answer: Concepts
Concepts concept RandomAccessIterator<typename X> : BidirectionalIterator<X>{ X& operator+=(X&, difference_type); X operator+(X, difference_type); X operator+(difference_type, X); X& operator-=(X&, difference_type); X operator-(X, difference_type); difference_type operator-(X, X); reference operator[](X, difference_type); } Define the ‘concept’ of a random access iterator:
Concepts template<typenameIter> requires : RandomAccessIterator<Iter>void sort(Iter begin, Iter end) { // ...} template<RandomAccessIteratorIter>void sort(Iter, Iter) { // ... } Define std::sort to require the RandomAccessIterator concept This is equivalent to:
Concepts template<typename T> concept_mapRandomAccessIterator<T*> { typedef T value_type; typedef std::ptrdiff_tdifference_type; typedef T& reference; typedef T* pointer; }; Concepts Maps – provide the mapping between concepts and real types
Concepts Want to know more: http://www.generic-programming.org/languages/conceptcpp/
Concurrency Problem: C++-03 assumes a single threaded environment but most new computers are multi-core. How to enable C++ developers to efficiently write multi-threaded code?
Concurrency Define a new C++ memory model Specify what it means to be a well formed C++ multi-thread application Will not change the runtime behavior of single threaded C++ applications
Concurrency • Add library support for threading primitives • lock/release/fences/etc. • Add higher level library support for features like thread pooling and futures (maybe lambdas?) • Open Issues • What to do about destructors/exceptions? • Semantics of thread cancellation
Garbage Collection struct S { }; void f(){ S* pS = new S();} The most “difficult” issue Enable GC for types that have a trivial destructor What is the interaction between code that expects GC and code that doesn’t?
Variadic Templates template<typename T1> tuple<T1> make_tuple(T1& t1); template<typename T1, typename T2> tuple<T1, T2> make_tuple(T1& t1, T2& t2); template<typename T1, typename T2, typename T3> tuple<T1, T2, T3> make_tuple(T1& t1, T2& t2, T3& t3); template<typename T1, typename T2, typename T3, typename T4> tuple<T1, T2, T3, T4> make_tuple(T1& t1, T2& t2, T3& t3, T4& t4); ... Works like variadic macros except on template parameters. Great for types like std::tuple
Variadic Templates template<typename... Types> tuple<Types...> make_tuple(Types&&...); With Variadic Templates there is just one make_tuple function:
Rvalue References void f(int&); void f(int&&); int i; f(i); // calls f(int&) f(1); // calls f(int&&) Introduces a new kind of reference that can bind to an rvalue Allows perfect forwarding (move semantics).
auto auto i = 4; std::vector<std::string> data; std::vector<std::string>::iteratoriter = data.begin(); auto iter = data.begin(); Finally a real use for ‘auto’
decltype inti; decltype(i) // int decltype((i)) // int& template<typename T1, typename T2> auto f(T1 t1, T2 t2) -> decltype(t1 + t2){ } decltype – what is the type of this variable or expression?
Other Features std::vector<std::list<int> > data; std::vector<std::list<int>> data; • Almost everything from TR-1 • The exception is the special math functions • C-99 Features • ‘long long’, variadic macros, __func__ • Allow ‘>>’ to terminate template argument lists
Timeline Oct 2008 – Committee Draft (CD) Oct 2009 – Final Committee Draft (FCD) Oct 2010 – Draft International Standard (DIS) Early 2011 – International Standard
Questions? Jonathan.Caves@microsoft.com