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

24,625

Solution 1

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.

Solution 2

If you want to use bind (or any other for the matter), a nice feature is namespace renaming, here is an example:

namespace MyNamespace = boost;

void DoSomething(void)
{
    MyNamespace::bind( ... );
}

Now, if you change MyNamespace to be:

namespace MyNamespace = std::tr1;

The following uses std::tr1::bind.

namespace MyNamespace = std::tr1;

void DoSomething(void)
{
    MyNamespace::bind( ... );
}

You should, of course, use MyNamespace for elements that you want to easily change it's namespace in the future, if you know you want std::tr1 you should use it directly and never an alias.

Solution 3

You've pretty much got it in your question there. I could just copy/paste your example and answer your question correctly. Only two thing really stand out as needing expansion:

1) Exactly HOW and why std:: is expanded by tr1. TR1 is "Technical Report 1" and is the first official set of library expansions proposed to the standards committee by one of its subgroups. So it's a little more than just an extension of the standard.

2) boost::bind actually behaves differently than std::bind, at least on some systems. I don't know if it's by standard on not but in MSVC lambda expressions and std::bind behave very poorly with each other. Maybe some other ways too, I don't recall since I made it policy to use boost::bind rather than std::bind. The return value template parameter seems to often be ignored with std::bind on msvc so that you get errors about there being no return_value<f>::type (or whatever) when you've specified it with std::bind<type>(...). Never bothered to figure out the exact difference in behavior since boost::bind had already made it into our regular vocabulary and we knew how to use it.

Solution 4

It shouldn't make a big difference since large parts of the next C++ standard were in fact inherited from Boost. So if you have std::bind and don't have to be compatible with other compilers, just use it.boost::bind is good if you want to be compiler-independent. I think std::tr1::bind doesn't have any advantages over the other two if they are available: it is nonstandard with respect to both C++03 and C++0x.

Share:
24,625
roxrook
Author by

roxrook

C++ is fun!

Updated on July 19, 2022

Comments

  • roxrook
    roxrook almost 2 years

    I initially thought they're all the same, but it turned out to be wrong. So can anyone briefly explain the differences between these three? For example:

    1. std::bind ( newest one, next generation of C++ )
    2. std::tr1::bind ( old, extension of C++ std )
    3. boost::bind ( completely separate library )

    or std::shared_ptr, std::tr1::shared_ptr, and boost::shared_ptr, ...etc

    Update

    bind, shared_ptr are examples that help to clarify my question. My intention was to understand the general differences between those three namespaces. There are several libraries that exist in all three namespaces, and apparently bind is one example, as well as shared_ptr.

    What namespaces should I stick with? I personally prefer library from std:: since it will be the next standard of C++ ( C++0x ).

  • wkl
    wkl over 13 years
    @VJo, yeah, but let's not pretend it's not going to be C++1x :p
  • wkl
    wkl over 13 years
    @ybungalobill +1 for making me laugh. :)
  • CashCow
    CashCow over 13 years
    The definitions were inherited from boost but the code implementation is not and I found horrible bugs in tr1::bind in 2009 where it didn't even allow you to pass ref() properly. My feeling is that they cannot just copy boost code into STL implementations and it is likely to be inferior as boost code has been peer-reviewed and tested over a long period, and therefore I personally out of choice would stick with boost.
  • roxrook
    roxrook over 13 years
    @CashCow: thanks for your advice. But how about libraries std:: from VC++? Are they worse than boost libraries? For example boost::regex vs std::regex?