197

I put my C++ skills on the shelf several years ago and it seems now, when I need them again, the landscape has changed.

We have got C++11 now, and my understanding is that it overlaps many Boost features.

Is there some summary where those overlaps lie, which Boost libraries going to become legacy, recommendation of which C++11 features to use instead of boost ones and which better not?

jotik
  • 17,044
  • 13
  • 58
  • 123
user377178
  • 2,363
  • 3
  • 16
  • 11
  • 4
    Boost was one of the first to implement the TR1 library. Since that's now in the standard, you should prefer the standard version. Boost.Lambda is also sort of replaced by actual lambdas now. – Kerrek SB Jan 13 '12 at 14:00
  • 6
    The [Wikipedia](http://en.wikipedia.org/wiki/C++11) article on C++11 has a good summary of most changes. – Some programmer dude Jan 13 '12 at 14:03

2 Answers2

292

Replaceable by C++11 language features or libraries

TR1 (they are marked in the documentation if those are TR1 libraries)

Features back-ported from C++11:

  • Atomic ← std::atomic
  • Chrono ← <chrono> (see below)
  • Move ← Rvalue references

Replaceable by C++17 language features:

  • String_ref → std::string_view
  • Filesystem<filesystem> (Filesystem TS)
  • Optional → std::optional (Library Fundamentals TS v1)
  • Any → std::any (Library Fundamentals TS v1)
  • Math/Special Functions<cmath> (Special Math IS), see the list below
    • beta function
    • (normal / associated / spherical) Legendre polynomials
    • (normal / associated) Legendre polynomials
    • Hermite polynomials
    • Bessel (J / Y / I / K) functions (Y is called Neumann function in C++)
    • spherical Bessel (j / y) functions
    • (incomplete / complete) elliptic integrals of (first / second / third kind)
    • Riemann zeta function
    • exponential integral Ei
  • Variant → std::variant (P0088R2)

The standard team is still working on it:

A large part of MPL can be trimmed down or removed using variadic templates. Some common use cases of Lexical cast can be replaced by std::to_string and std::stoX.

Some Boost libraries are related to C++11 but also have some more extensions, e.g. Boost.Functional/Hash contains hash_combine and related functions not found in C++11, Boost.Chrono has I/O and rounding and many other clocks, etc. so you may still want to take a look at the boost ones before really dismissing them.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 1
    Add to the list [Boost.Chrono](http://www.boost.org/libs/chrono/), [Boost.Exception](http://www.boost.org/libs/exception/), and [Boost.Swap](http://www.boost.org/libs/utility/swap.html). – ildjarn Jan 13 '12 at 17:59
  • 9
    Note that Boost.Lambda (or rather, Boost.Phoenix' lambdas), are still useful for polymorphic lambdas. – Xeo Jan 13 '12 at 18:00
  • 2
    Nice list, although I do not believe `std::unique_ptr` is part of TR1 (since it requires move semantics) – Nemo Jan 13 '12 at 18:07
  • 1
    @ildjarn: Boost.Chrono provides much more functions than . Boost.Exception — only N2179 is relevant. – kennytm Jan 13 '12 at 18:41
  • 2
    @Nemo: Yes. Only std::tr1::shared_ptr is part of TR1, and const std::unique_ptr replaces the use cases of boost::scoped_ptr and boost::scoped_array – kennytm Jan 13 '12 at 18:43
  • Minor note, but `boost::swap<>`'s original purpose was not to swap arrays, but to do ADL for `swap` on your behalf instead of having to use `using`; swapping arrays was added later as a bonus essentially. – ildjarn Jan 13 '12 at 20:13
  • 1
    Could add boost::non_copyable and ptr_container to this list. See http://stackoverflow.com/a/7841332/297451 – Jon Nov 06 '13 at 01:22
  • Can you elaborate on why boost::intrusive_ptr cannot be replaced? – D. A. Jul 24 '14 at 19:20
  • I'm finding that I use boost::string_ref a lot – paulm Jan 12 '15 at 15:51
  • Started linking the C++11 constructs to their cppreference.com pages... this is a really useful list, let's make it even more useful! – einpoklum Feb 20 '15 at 19:55
  • @kennytm This might be an edge case but on a `boost::scoped_ptr` you can call `reset()` while on a `std::unique_ptr const` you can't. – Brandlingo Mar 09 '15 at 10:51
  • You may note that ASIO is also available without boost – MicroCheapFx Aug 19 '16 at 13:24
  • 1
    Someone should really maintain a table whose rows are Boost libraries/features, and whose columns correspond to coverage by C++ standards / standards+TSes. When some functionality remains uncovered it could either be a different color or a comment. Like on Wikipedia pages with feature comparison tables. – einpoklum Feb 10 '18 at 21:59
  • @kennytm `gcd` and `lcm` are now in C++17. – L. F. Mar 25 '19 at 13:15
55

Actually, I don't think the boost libraries are going to become legacy.

Yes, you should be able to use std::type_traits, regex, shared_ptr, unique_ptr, tuple<>, std::tie, std::begin instead of Boost Typetraits/Utility, Boost Smartpointer, Boost Tuple, Boost Range libraries, but there should in practice be no real need to 'switch' unless you are moving more of your code to c++11.

Also, in my experience, the std versions of most of these are somewhat less featureful. E.g. AFAICT the standard does not have

  • Perl5 regular expressions
  • call_traits
  • Certain regex interface members (such as bool boost::basic_regex<>::empty()) and othe interface differences
    • this bites more since the Boost interface is exactly matched with Boost Xpressive
    • and it plays much more nicely with Boost String Algorithms Obviously, the latter don't have standard counterparts (yet?)
  • Many things relating to TMP (Boost Fusion)
  • Lazy, expression template-based lambdas; they have inevitable benefits in that they can be polymorphic today, as opposed to C++11. Therefore they can often be more succinct:

     std::vector<int> v = {1,2,-9,3};
    
     for (auto i : v | filtered(_arg1 >=0))
         std::cout << i << "\n";
    
     // or:
     boost::for_each(v, std::cout << _arg1);
    

    Most definitely, this still has some appeal over C++11 lambdas (with trailing return types, explicit capturing and declared parameters).

Also, there is a BIG role for Boost, precisely in facilitating path-wise migration from C++03 to C++11 and integrating C++11 and C++03 codebases. I'm particularly thinking of

  • Boost Auto (BOOST_AUTO)
  • Boost Utility (boost::result_of<> and related)
  • Boost Foreach (BOOST_FOREACH)
  • Don't forget: Boost Move - which makes it possible to write classes with move semantics with a syntax that will compile equally well on C++03 compilers with Boost 1_48+ and C++11 compilers.

Just my $0.02

sehe
  • 374,641
  • 47
  • 450
  • 633