Your SlideShare is downloading. ×
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
An Overview Of Standard C++Tr1
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

An Overview Of Standard C++Tr1

1,508

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,508
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. An overview of Standard C++ TR1 S G Ganesh
  • 2. Agenda  Introduction to TR1  Utility Classes  Containers  Function Objects  Type Traits  Numerics  Regular Expressions  C-compatibility  Compilers/Library Vendors Supporting TR1 09/06/08 2
  • 3. Introduction To TR1  TR1 - quot;Technical Report 1”  New functionality added to C++ Standard Library  Provided in std::tr1 namespace  Not part of the C++ Standard (yet)  Most of the vendors don’t support it (yet)  Most of the TR1 components from Boost  Only essential components are included  Fills gaps left in the standard library  not “less important”, “interesting” or “curious” components  Expanded toolkit - many useful reusable components  Helps you to be more productive  Helps you avoid reinventing the wheel 09/06/08 3
  • 4. Utilities – shared_ptr  tr1::shared_ptr  smart pointer for shared memory resource  Automates resource(memory) deallocation  Not a scoped pointer like std::auto_ptr  Can be copied freely  Can be used in containers  Reference counted  Overhead involved as need to maintain a counter  Circular references are a trouble (they won’t be freed) • Use tr1::weak_ptr for that  tr1::shared_ptr complements std::auto_ptr 09/06/08 4
  • 5. Shared_ptr - Example #include <tr1/shared_ptr> #include <vector> #include <iostream> using namespace std; using namespace std::tr1; struct Object { Object(int val) { mem = val; cout << quot;Creating Objectquot; << mem; } ~Object() { cout << quot;Destroying Objectquot; << mem; } int mem; }; int main() { typedef shared_ptr<Object> ObjectPtr; vector<ObjectPtr> SmartVector; SmartVector.push_back(ObjectPtr(new Object(1))); SmartVector.push_back(ObjectPtr(new Object(2))); } // prints: Creating Object1 Creating Object2 Destroying Object1 Destroying Object2 09/06/08 5
  • 6. Utilities – Tuple  Generalization of the template std::pair  std::pair is for two types, tr1::tuple is for N types  N can be up to 10 or more  make_tuple, that creates a tuple object  similar to make_pair  However syntax of tuple is different that of pair  since first and second doesn’t scale  so, use function template get<n> instead  Useful for:  Eliminating boilerplate container classes  Particularly useful for multiple returning values from a function  Storing more than one object in a container 09/06/08 6
  • 7. tuple - Example #include <tr1/tuple> #include <iostream> using namespace std; using namespace tr1; using namespace tr1::tuples; tuple<char, int, float> tuple_creator() { return make_tuple ('b', 3, 3.0f); } int main() { tuple<char, int, float> my_tuple ('1', 1, 1.0f); get<0>(my_tuple) = 'a'; get<1>(my_tuple) = 2; get<2>(my_tuple) = 2.0f; cout << set_open('{') << set_delimiter('-') << set_close('}') << my_tuple << tuple_creator() << endl; } 09/06/08 // prints: {a-2-2} {b-3-3} 7
  • 8. Containers  Hurray, at last, we have Hash maps!  Standard associative containers are ordered (typically implemented as RB trees)  Accessing an element is slower  Unordered associative containers is a dire need  Hashmaps promise constant time for element access  tr1::unordered_set, tr1::unordered_multiset, tr1:: unordered_map, tr1::unordered_multimap  Might have better naming convention  but “unordered” instead of “hash” abstracts implementation details 09/06/08 8
  • 9. Containers – Array  Fixed Size Container  Use when you know how many objects you need  Satisfies requirements for a sequence container  iterators, front(), back(), size() …  So you can use them with standard algorithms  Safe to pass/return them in functions easily and safely  C style arrays error prone and strict no no!  Use tr1::array instead if no need to interface with legacy code 09/06/08 9
  • 10. Containers – Array - Example #include <tr1/array> #include <iostream> using namespace std; int main() { typedef tr1::array<double, 5> dbl_fixed_array; dbl_fixed_array my_array; dbl_fixed_array::iterator itr = my_array.begin(); while(itr != my_array.end()) *itr++ = 3.0; for(int i = 0; i < my_array.size(); i++) cout << my_array[i]; // prints 3 3 3 3 3 } 09/06/08 10
  • 11. Function Objects  Four New Function Objects  Named Function Objects  tr1::reference_wrapper and tr1::function  Unnamed Function Objects  Produced by calls to tr1::mem_fn and tr1::bind 09/06/08 11
  • 12. Function Objects - reference_wrapper  Wrapper type that acts like a reference object  But unlike references, tr1::reference_wrapper objects can be copied and assigned to  ref(x) creates an tr1::reference_wrapper<T> object • where T is type of x  Use cref(x) for creating tr1::reference_wrapper<const T> 09/06/08 12
  • 13. Function Objects – reference_wrapper - Example #include <boost/ref.hpp> #include <functional> #include <iostream> using namespace boost; using namespace std; struct functor { int operator()(int x, int y) { return x * y; }; }; template <class Func> void apply(Func func, int arg1, int arg2) { cout << func(arg1, arg2); } int main() { reference_wrapper<functor > rw1 = ref(functor()); apply<functor>(rw1, 10, 20); reference_wrapper<multiplies<int> > rw3 = ref(multiplies<int>()); apply<multiplies<int> >(rw3, 10, 20); } 09/06/08 13
  • 14. Function Objects - function  Wrapper type for a type that has function call operator  e.g. function<int (int, float)>  This refers to a type with function call operator which can take an int and a float and return an int  Store any function-like ‘thing’: functions, member functions, functors  First-class objects: Easily pass them to functions or copy  Powerful because it can abstract object of any type  that which has a function call operator of specific arguments and return types  Store an action  Call at any time; change at any time  Implementation of the GoF Command pattern 09/06/08 14
  • 15. Function Objects – function - Example #include <tr1/function.hpp> #include <iostream> using namespace std; using namespace std::tr1; struct mul_functor { float operator()(int x, int y) const { return ((float) x) * y; }; }; void call(function<float (int, int)> &f, int x, int y) { cout << f(x, y); } int main() { function<float (int, int)> func; func = mul_functor(); call(func, 100, 200); } // prints: 20000 09/06/08 15
  • 16. Function Objects – mem_fn and bind  Produced by calls to tr1::mem_fn and tr1::bind  tr1::mem_fn  generalized wrapper for pointer to functions  first argument is the object on which call is done  can be an object, a reference, a pointer, or a smart pointer!  remaining are named arguments to the member function  tr1::bind  generalization of std::bind1st and std::bind2nd  Why unnamed function objects?  specifies template functions that return objects of these new types  because we use these as function objects that we pass to template functions 09/06/08 16
  • 17. Function Objects – mem_fn - Example #include <tr1/function> #include <tr1/shared_ptr> #include <iostream> #include <memory> using namespace tr1; using namespace std; struct some_struct { void print() { cout << quot;some_struct::print quot;; } }; template <class function> void call(function fn) { // call through call wrapper fn(new some_struct()); fn(shared_ptr<some_struct>(new some_struct())); fn(shared_ptr<some_struct>(new some_struct()).get()); } int main() { call(mem_fn(&some_struct::print)); // prints: some_struct::print some_struct::print some_struct::print } 09/06/08 17
  • 18. Function Objects – bind - Example #include <tr1/bind> #include <iostream> using namespace std; using namespace tr1; template <class Binder> void show(Binder binder) { int i = 5; int j = 10; binder(i, j); } void print(int t) { cout << t << 't'; } int main() { show(bind(print, 0)); show(bind(print, _1)); show(bind(print, _2)); } // prints: 0 5 10 09/06/08 18
  • 19. Type Traits  Useful for template programming  instead of knowing nothing about the magic type “T” passed as type parameter in a template  provides a set of “type predicates”  can do “type transformations” when we have knowledge about what exactly is the type “T” in a template  Template meta-programming  Like it or not, template meta-programming is getting wide attention  many of the things that can be done with type traits can also be done with function overloading  but typetraits simplify things and makes meta-programming easier 09/06/08 19
  • 20. Type Traits - Example #include <tr1/type_traits> #include <cstring> #include <iostream> using namespace std; using namespace tr1; struct do_copy { template<typename T> static void exec(T* dest, T* src, int num) { if(!is_pod<T>::value) { for(int i=0; i<num; ++i) *dest++ = *src++; } else memcpy(dest, src, sizeof(T)*num ); } }; 09/06/08 20
  • 21. Type Traits – Example continued struct S { S& operator = (const S &s) { cout << quot;copying Squot;<< endl; return s; } }; int main() { do_copy c; S * sarr1 = new S[3]; S sarr2[3]; char str1[6] = quot;helloquot;; char str2[6] = quot;worldquot;; c.exec(str2, str1, sizeof(str1)); cout << str2; c.exec(sarr2, sarr1, 3); } // prints: hello copying S copying S copying S 09/06/08 21
  • 22. Numerics  Mathematical special functions  twenty-three functions  float, double, and long double overloads  Engineering and scientific computing becomes easier  No reinventing the wheel/no writing of textbook code  For established and widely used functionality (in specific domains) e.g: cylindrical Bessel functions, confluent hypergeometric functions  Four random number generators and nine types of binomial distributions  You can combine those generators and distributions 09/06/08 22
  • 23. Regular Expressions  Finally we have pattern matching capabilities  Powerful search and replace features  use tr1::regex to store a regular expression  pass the tr1::regex object to tr1::regex_match, tr1:: regex_search, or tr1::regex_replace  Templated thingy, so not limited to searching standard strings  Perl can no more boast that it’s the best  Use iterators to step through the subsequences/matches for the regular expression 09/06/08 23
  • 24. Regular Expressions - Example #include <tr1/regex> #include <iostream> using namespace std; using namespace tr1; int main() { //regex_merge example const string one(quot;tr1 quot;); const regex two(quot;stuff is quot;); const string three(“coolquot;); cout << regex_merge(one, two, three); // prints: tr1 stuff is cool // regex_match example const string good_str (“4323-4342quot;); const string bad_str (“645433-323quot;); const regex match_expr(quot;(d{4}-d{4})quot;); cout<< boolalpha << regex_match(good_str, match_expr) << regex_match(bad_str, match_expr) << endl; // prints: true false } 09/06/08 24
  • 25. C Compatibility  TR1 adopts the changes in C standard  C++ standard is based on 1990 C standard  C standard amended in 1995; revised in 1999 (C99)  Adopted features from C standard  Low-level control of floating point operations  Several new functions from C standard library  fixed width integral types  new format specifiers for printf/scanf family  New argument matching rules for math functions as in C standard  Currently ambiguous: for call to atan2(flt, dbl), atan2(float, float), atan2(double, double), and atan2(long double, long double) equally good matches  Under new rules atan2(double, double) is the best match 09/06/08 25
  • 26. Compilers/Vendors Supporting TR1  Dinkumware (www.dinkumware.com)  first commerical library vendor to provide TR1 (as part of the Dinkum C++ Library)  Boost (www.boost.org)  Many of the components of TR1 from Boost library  A separate download package of TR1 components already available  Project GNU (www.gnu.org)  Parts of TR1 in their C++ Standard Library  Can be used with g++  HP aCC  RogueWave doesn’t provide TR1 yet  Testing of Boost TR1/aCC planned  with sample programs, the components seem to work fine 09/06/08 26
  • 27. Wrapping Up  Links  The standard committee document on TR1 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1  The Boost TR1 http://freespace.virgin.net/boost.regex/tr1/index.html  Q&A  Thank You! 09/06/08 27

×