How does one include TR1?
Asked Answered
D

6

29

Different compilers seem to have different ideas about TR1. G++ only seems to accept includes of the type:

#include <tr1/unordered_map>
#include <tr1/memory>
...

While Microsofts compiler only accept:

#include <unordered_map>
#include <memory>
...

As for as I understand TR1, the Microsoft way is the correct one.

Is there a way to get G++ to accept the second version? How does one in general handle TR1 in a portable way?

Decrial answered 4/8, 2009 at 16:5 Comment(1)
TR1 is not actually part of the C++ Standard, so the "correct" way of including these non-standard header files must be moot.Postbox
C
17

Install boost on your machine.
Add the following directory to your search path.

<Boost Install Directory>/boost/tr1/tr1

see here boost tr1 for details

Now when you include <memory> you get the tr1 version of memory that has std::tr1::shared_ptr and then it includes the platform specific version of <memory> to get all the normal goodies.

Cannell answered 4/8, 2009 at 17:35 Comment(2)
+1. Since TR1 appears to be "lets import these boost classes into the standard"Veinlet
I do #include "boost/tr1/unordered_map.hpp" and it uses the tr1 implementation if it is available.Natasha
I
14
#ifdef _WIN32
    #include <unordered_map>
    #include <memory>
#else
    #include <tr1/unordered_map>
    #include <trl/memory>
#endif
Inhibitor answered 15/7, 2010 at 20:20 Comment(3)
You should probably base the conditional compilation on the compiler, not the platform (_WIN64 is real): #if defined( _MSC_VER ) && ( _MSC_VER > 1300 ), for example.Eamon
On last include, tr1 is misspelled as trL ;)Inferential
If you are doing std::tr1::unordered_map , do you still need to include both <tr1/unordered_map> and <tr1/memory> or can you include only <tr1/unordered_map>? Trying to reduce the amount of libraries included.Metcalfe
P
4

Perhaps the best way would be to simply use boost libraries for now, as in many cases they have alternatives with a similar interface to TR1 features, and are just in a different (but consistent) header path and namespace. This has the advantage of working on compilers that haven't even begun implementing C++0x. And there are plenty of useful boost libraries that aren't in TR1 at all :)

Alternately, on G++, you could try passing --std=gnu++0x on the command line. This works for <unordered_set> and <unordered_map>, at least. Then to make it available in std::tr1:

namespace std { namespace tr1 { using namespace std; } }

This is evil, naturally. I highly recommend the boost approach instead :)

Parasitize answered 4/8, 2009 at 16:8 Comment(2)
Using -std=c++0x or -std=gnu++0x doesn't work as it will only make std::unordered_set<> available, but not std::tr1::unordered_set<>.Decrial
@Grumbel, updated with an evil hack that will make it available in std::tr1Parasitize
A
3

A tad hacky perhaps, but you could simply add the compiler tr1 directory to your include path.

Actinomycosis answered 4/8, 2009 at 16:8 Comment(0)
R
2

If under Windows, add the 'tr1' directory to the system path. Then #include <memory> should work.

Richly answered 4/8, 2009 at 16:9 Comment(1)
do compilers on windows use the system path as well as the include path when resolving includes?Actinomycosis
T
2

I asked myself the same question. Unfortunately, the technical report doesn't say how the headers should be included. It only defines that the extensions should be in the ::std::tr1 namespace.

Tiossem answered 21/9, 2009 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.