1. Let’s Take A Look At The
Boost Libraries
Seoul System Programmers Network #6
@TomSmartBishop Thomas Pollak
2. Setup Boost (currently v1.61)
Download and unzip (http://www.boost.org/users/download/)
Run bootstrap(.bat|.sh)
Run b2 to generate the libs for your toolchain
Without parameters b2 will try to auto-detect your toolchain
In case you would like to build for another platform or b2 has troubles use --help to
see all options
Libs will be in the sub directory stage/lib, you can move/install everything to the
default locations with b2 install
3. Let’s look at the samples
You can find the source code here:
https://github.com/TomSmartBishop/sspn_06_lets_boost
I picked 7 boost libraries that I found interesting and used some more as little helpers,
but there is much more: http://www.boost.org/users/history/version_1_61_0.html
Most libraries are header only and make heavy use of templates (and macros). Samples
that need library linkage are pointed out on the slides.
Be aware that there s a compile time overhead, also for small samples.
Due to the fact that most boost libraries have different authors the API seems not always
homogenous (however there are core components used across all libs).
Sometimes the API feels sometimes a bit over-engineered but does work as expected
(compared to the STL lib).
In VS2015 /Wall produces a lot warnings so that /W4 seems like a better choice.
4. Boost Program Options
http://www.boost.org/doc/libs/1_61_0/doc/html/program_options.html
In case you are writing a program with command line parameters this is quite useful.
No need to re-implement boring stuff again and again. The github sample uses the
program options also to switch between the other sample implementations.
Needs to be linked against the program_options library.
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level");
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if (vm.count("help"))
cout << desc << "n";
else if (vm.count("compression"))
cout << "Compression level was set to " << vm["compression"].as<int>() << ".n";
else
cout << "Compression level was not set.n";
5. Boost Any
http://www.boost.org/doc/libs/1_61_0/doc/html/any.html
This behaves more or less what we know from Java and C# as “object”. It uses a
template class to store the exact type you using. Therefore no implicit conversions,
if you assign 5, the type will be an int and if you assign 5.0 it will be a double.
Boost Any is really handy and interestingly has been described in a paper back in
the year 2000, however it’s not part of the STL (would be nice though).
std::vector<boost::any> values;
values.push_back(std::string("Hello"));
values.push_back("World");
values.push_back(42);
values.push_back(3.14f);
6. Boost Variant
http://www.boost.org/doc/libs/1_61_0/doc/html/variant.html
Similar to Any, but Variant accepts only the predefined types. So it looks more like
a union, but without the drawback of a union.
Drawbacks of a union?
You can access a union at any time with any variation, eg. access the a float’s data as an
int and this behavior is not defined in the standard. The standard only specified
subsequent access of the same variant. Interestingly this is supported by most compilers
(at least MSVC, clang, GCC) and is actually used in real world applications to access the
bit representation of floating point number (or similar).
Only POD types allowed, but boost Variant also allows non PODs so we can create a
variant like this: boost::variant< int, std::string > var("hello world");
7. Boost Lexical Cast
http://www.boost.org/doc/libs/1_61_0/doc/html/boost_lexical_cast.html
Convenient type conversion, no more need for atoi or sprintf to convert from
int to string or the other way around.
Supports all fundamental C++ types and much more convenient compared to
std::strtol and similar: int32_t i = lexical_cast<int32_t>("42");
Heap allocation can be avoided if desired (when converting to string).
8. Boost Signals2
http://www.boost.org/doc/libs/1_61_0/doc/html/signals2.html
Callbacks with multiple targets. Comparable with event listeners in Java (without
interfaces).
The callback receivers are called “slots” which are invoked by the “signal”.
struct HelloWorld
{
void operator()() const { cout << "Hello, World!n"; }
};
// Signal with no arguments and a void return value
boost::signals2::signal<void ()> sig;
// Connect a HelloWorld slot
HelloWorld hello;
sig.connect(hello);
// Call all of the slots
sig();
9. Boost Serialization
http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/index.html
Easy serialization, even though I cannot speak of a large scale experience, the interface
looks good and I couldn’t spot any obvious pitfalls.
Can handle STL containers and as far as I could see also other boost classes like
boost::greogorian::date.
Can restore pointers (serializes the content, not the pointer).
Needs to be linked against the serialization library
(as well as the chrono/date_time library).
Uses RTTI by default, but also can be used without RTTI (with some typing overhead).
10. Boost Compute
http://www.boost.org/doc/libs/1_61_0/libs/compute/doc/html/index.html
Compute is one of the latest additions to boost. In my small samples I also got
two warning during “normal” API usage, which I could fix by myself by doing some
explicit type casts in the library’s header, so it looks like it needs still some time to
really get stable.
However the concept is quite interesting: You can use your GPU to perform
computations and the API let’s you do that in a very convenient way without
worrying about the underlying hardware or compute shaders.
I didn’t perform any tests how much that can speedup operations, so I still need
some more time to play around and explore…