Professional Documents
Culture Documents
09/06/08 2
Introduction To TR1
TR1 - "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
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
Shared_ptr - Example
#include <tr1/shared_ptr>
#include <vector>
#include <iostream>
struct Object {
Object(int val) { mem = val; cout << "Creating Object" << mem; }
~Object() { cout << "Destroying Object" << 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
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
tuple - Example
#include <tr1/tuple>
#include <iostream>
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;
int main() {
typedef tr1::array<double, 5> dbl_fixed_array;
dbl_fixed_array my_array;
09/06/08 11
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
Function Objects – reference_wrapper -
Example
#include <boost/ref.hpp>
#include <functional>
#include <iostream>
struct functor {
int operator()(int x, int y) { return x * y; };
};
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
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
Function Objects – function -
Example
#include <tr1/function.hpp>
#include <iostream>
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
Function Objects – mem_fn and bind
09/06/08 16
Function Objects – mem_fn -
Example
#include <tr1/function>
#include <tr1/shared_ptr>
#include <iostream>
#include <memory>
int main() {
call(mem_fn(&some_struct::print));
// prints: some_struct::print some_struct::print some_struct::print
}
09/06/08 17
Function Objects – bind - Example
#include <tr1/bind>
#include <iostream>
int main() {
show(bind(print, 0));
show(bind(print, _1));
show(bind(print, _2));
} // prints: 0 5 10
09/06/08 18
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
Type Traits - Example
#include <tr1/type_traits>
#include <cstring>
#include <iostream>
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
Type Traits – Example continued
struct S {
S& operator = (const S &s) { cout << "copying S"<< endl; return s; }
};
int main() {
do_copy c;
S * sarr1 = new S[3];
S sarr2[3];
char str1[6] = "hello";
char str2[6] = "world";
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
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
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
09/06/08 23
Regular Expressions - Example
#include <tr1/regex>
#include <iostream>
using namespace std;
using namespace tr1;
int main() {
//regex_merge example
const string one("tr1 ");
const regex two("stuff is ");
const string three(“cool");
cout << regex_merge(one, two, three); // prints: tr1 stuff is cool
// regex_match example
const string good_str (“4323-4342");
const string bad_str (“645433-323");
const regex match_expr("(\\d{4}-\\d{4})");
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
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