How to fix your code to build with C++11

Most of the code can compile out of the box when the special gcc flag -std=c++11 (or -std=c++0x) is used, but some tricky constructs do not work (essentially Boost). In this page I'm collecting instructions on how to fix the most common cases.

boost::assign::list_of

(permalink)

It's quite handy to initialize vectors with boost::assign::list_of, but it does not always work with C++11.

The easiest work-around is to use a temporary variable. wrap the code in an #ifdef block, to replace it with the new inlitilizer list syntax available with C++11.

old code

// declared as "std::vector<MyType> m_somedata;" in the header.
  m_somedata = boost::assign::list_of(value1)(value2)(value3);

new code

std::vector<MyType> tmp = boost::assign::list_of(value1)(value2)(value3);
  m_somedata = tmp;

It must be noted that it is possible to initialize a vector with many times the same value without Boost:

old code

std::vector<MyType> v = boost::assign::list_of(value)(value)(value);
  v = boost::assign::list_of(value1)(value1)(value1)(value1);

new code

std::vector<MyType> v(3, value);
  v = std::vector<MyType>(4, value1);

References

boost::assign::map_list_of

(permalink)

The problem with map_list_of is essentially the same as with list_of and the solution is similar:

old code

std::map<KeyType, ValueType> m = boost::assign::list_of(key1, value1)(key2, value2)(key3, value3);

new code

#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
  std::map<KeyType, ValueType> m = {{key1, value1}, {key2, value2}, {key3, value3}};
#else
  std::map<KeyType, ValueType> m = boost::assign::list_of(key1, value1)(key2, value2)(key3, value3);
#endif

References

std::make_pair

(permalink)

With the new C++ standard, it may happen that the following code does not compile:

... std::make_pair<Type1, Type2>(value1, value2) ...

To fix it, you have to remove the template arguments:

... std::make_pair(value1, value2) ...
or, if the types of the arguments are not correct,
... std::make_pair(static_cast<Type1>(value1), static_cast<Type2>(value2)) ...

References

boost::bind

(permalink)

Boost bind functors are a practical way to pass simple functors to STL algorithms, but it does not work with C++11, which provides its own alternative.

The way to make your code compile with C++11 enabled and disabled depends on how you include and use boost::lambda.

If you have in your code using namespace boost::lambda, the easiest is to format your include like this:

#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
#include <functional>
using std::bind;
using namespace std::placeholders;
#else
#include <boost/lambda/bind.hpp>
using namespace boost::lambda;
#endif
then you can use bind safely, for example, like this:
int count = std::count_if(v.begin(), v.end(), bind(&ValueType::test,_1));

Warning: std::bind does not support logical operators (as boost::lambda::bind does), so if you rely on it, you have to modify your code like in the following example

old code

std::stable_sort(v.begin(), v.end(), bind(&ValueType::test,_1) < bind(&ValueType::test,_2));

new code

std::stable_sort(v.begin(), v.end(), bind(std::less<TestReturnType>(), bind(&ValueType::test,_1), bind(&ValueType::test,_2));

References:

Advanced use of boost::lambda

(permalink)

In the cases the use of boost::lambda tricks is more advanced than a simple boost::lambda::bind, you have to consider the adoption of the new lambda functions.

References:

-- MarcoClemencic - 23-Sep-2012

Edit | Attach | Watch | Print version | History: r8 < r7 < r6 < r5 < r4 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r8 - 2012-11-26 - MarcoClemencic
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    LHCb All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback