What Are Differences Between Std, Tr1 and Boost (As Namespaces And/Or Libraries)

What are differences between std, tr1 and boost (as namespaces and/or libraries)?

1 - std::bind is the the standard name for it. This will be the name you use for C++11 compliant libraries. List of all libraries in standardized C++.

2 - std::tr1::bind is C++ Technical Report 1 namespace. Between C++03 and C++11 there was the C++ Technical Report 1, which proposed additional libraries and enhancements. Most of these already existed in Boost at the time, and some of these library changes were adopted in the C++11 standard, like <regex> and <functional> (which contains std::bind). The std::tr1 namespace was used to differentiate the libraries in their work-in-progress state, as opposed to everything standardized in the std namespace.

3 - boost::bind is for bind in the boost namespace, if you are using the Boost library. Boost encompasses much more than what is in TR1 and what i in C++11's std library. List of all libraries in Boost as of 1.52.0

Most of what was in TR1 has been standardized and is in the C++11 std namespace, and C++11 contains more libraries than mentioned in TR1 that were adapted from Boost constructs, like threading support defined in <thread>.

Part of what defines what you can use and which namespace you can use now depends on your compiler. I don't recall, but I think the more recent GCC-g++ implementations have started using std namespaces for the new C++11 libraries, but might require a different compiler flag to activate that. They will still support the std::tr1 namespace though. Visual C++ 2010 moved what was previously in std::tr1 into the normal std namespace, but Visual C++ 2008 still used std::tr1.

Are there important differences to know about between boost::function and std::tr1::function

What did you read? What do you mean by tricky? Are you referring to some ancient compilers only supporting the boost::function1<void, int> form rather than boost::function<void(int)> form?

std::tr1::function should be provided by your compiler, so if your compiler provides it at all then it should work perfectly, and not be "tricky" (whatever you mean by that.)

Asking if there are differences between Boost's specific implementation and an interface specification, which is not an implementation, doesn't make sense. One implementation could be similar to Boost's and have no substantial differences and another implementation could be completely different. GCC's std::tr1::function was contributed by the author of Boost.Function, so it's very similar.

Should we prefer Boost or standard lib?

I think you should use standard lib when available because... it's standard and comes with the compiler. Besides, if you use boost you need an annoying external dependency.

So, my advice is: use std when possible. If you're writing portable code, that must also be compiled with old compilers, you can consider to use your own namespace (e.g.: cxx0x) that embeds std or boost namespace according to the compiler you're using (this is called namespace alias):

#ifdef COMPILER_HAS_CXX0X
#include <memory>
namespace cxx0x = std;
#else
#include <boost/shared_ptr.hpp>
namespace cxx0x = boost;
#endif

...

cxx0x::shared_ptr<MyClass> = ...

Can I use C++ TR1 in VS2010?

VS 2010 supports TR1 out of the box. You don't need a tr1/ at the beginning of the file name when you include it though.

#include <functional>

typedef std::tr1::function<void(int)> MyFunction;

Note that TR1 doesn't specify a file name for the headers, so as far as conforming with TR1 goes, either one is about the same as the other.

Using TR1 libraries in GCC and MSVC

OK, after having several inconsistent and unsurmountable problems with Boost.TR1, especially when trying to use GCC's native TR1 libraries, I decided to ditch Boost entirely and use a small #define workaround. Here is my "tr1.h":

#ifndef _TR1_INCLUDE_H
#define _TR1_INCLUDE_H

/** Usage: #include TR1INCLUDE(unordered_map)
**
** Configuration: Define HAVE_TR1_SUBDIR if you need #include <tr1/unordered_map>; otherwise we take #include <unordered_map>.
**
**/

#define QUOTE(arg) <arg>

#ifdef HAVE_TR1_SUBDIR
# define TR1IFY(arg) tr1/arg
#else
# define TR1IFY(arg) arg
#endif

#define TR1INCLUDE(arg) QUOTE(TR1IFY(arg))

#endif

Now I can just write my programs like this:

#include "tr1.h"
#include TR1INCLUDE(unordered_map)

tr1: boost vs vs2010, using shared_ptr without namespace

Don't put using namespace in headers, as you've discovered it can break headers that follow, and because you can't change those headers there isn't much you can do about it.

At function scope you can use a using declaration to disambiguate:

void f()
{
using std::tr1::shared_ptr;
shared_ptr<int> p;
}

But that won't work in the global namespace, because you've already polluted that scope with the careless using directives.

what will happen with the overlapping portion of boost once C++0x becomes mainstream?

One would hope that Boost continues to support existing classes, for a couple of reasons.

First, there is a body of code that uses the overlapping features in Boost that needs to be supported, for some time.

Second, overlapping implementations allow me to select which one I'd prefer to use. There might be some difference between std::Frob and Boost::Frob that is important to my project, and having a choice is good.

In the long term, though, I would expect a migration toward the standard from both the application writers and the tools providers. That makes it a less risky choice to go with std::.

What namespace identifiers are in Boost and Qt libraries?

Yes, Boost libraries generally put their stuff in boost namespace or subnamespaces of it. Some might define global symbols, but the documentation will tell you if that's the case.

Qt's use of namespaces is very limited. It prefers to prefix all of its symbols with Q or Qt, although it uses Qt namespace for some identifiers, and maybe some others.



Related Topics



Leave a reply



Submit