Getting Started with TR1


 
My topic at GDC was Technical Report 1 (TR1), an "informative document" that includes new C++ library features that are highly likely to become a part of the next C++ Standard. You can download versions of TR1 from the following places:
Using TR1 is easy. Just include the header file that contains the feature you want, qualify the namespace by using std::tr1, and away you go. Here’s a quick example:
#include <array> // new fixed-size array container
std::tr1::array< int, 4 > a; // fully qualified name; note size is template param
 
// more common usage
using namespace std::tr1;
array< int, 4 > a = { 1,2,3,4 }; // cool — you can even initialize using standard array initialization
If you’ve ever been frustrated that you couldn’t use C-style arrays in a similar fashion as other C++ containers, tr1:array is the container for you. Now you can do things like sort:
std::sort( a.begin(), a.end() );
and size extraction:
for( int i = 0; i < a.size(); ++i )
just as if the array was any old STL container. Naturally tr1::array performance is the same as for C-style arrays. For example, a[x] produces the exactly the same code when ‘a’ is a tr1::array as when ‘a’ is an old-fashioned C-array.
 
However, TR1 arrays aren’t true containers. For instance, they can’t grow (no resize or reserve or push_back). And swaps of arrays (array.swap()) are not constant time operations like they are for vector, deque, list and so forth — array swaps are linear.
 
Nevertheless, I’ll be using TR1-style arrays much more than C-style arrays in my own code. All the benefits far outweigh the minor drawbacks.
 
Advertisements
This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s