Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost 1.53 local date time compiler error with -std=c++0x

Tags:

c++

c++11

g++

boost

Using g++ version 4.7.2, if I try compiling the following

#include <boost/date_time/local_time/local_time.hpp> 

class Bar
{
public:

Bar() { tz_db_.load_from_file("/home/date_time_zonespec.csv"); }

private:
    boost::local_time::tz_database tz_db_;
};

int main()
{
    return 0;
}

with -std=c++0x I get the following error.

In file included from /usr/local/include/boost/date_time/local_time/local_time_types.hpp:18:0,
                 from /usr/local/include/boost/date_time/local_time/local_time.hpp:13,
                 from test.h:4,
                 from test.cpp:1: /usr/local/include/boost/date_time/local_time/custom_time_zone.hpp: In instantiation of ‘bool boost::local_time::custom_time_zone_base<CharT>::has_dst() const [with CharT = char]’: test.cpp:11:1:   required from here /usr/local/include/boost/date_time/local_time/custom_time_zone.hpp:67:30: error: cannot convert ‘const boost::shared_ptr<boost::date_time::dst_day_calc_rule<boost::gregorian::date>
>’ to ‘bool’ in return

If I leave off the c++0x option, everything is fine. Can anybody tell me what's going on here?

like image 821
Craig Avatar asked Mar 05 '13 21:03

Craig


1 Answers

When you build for C++11, boost::shared_ptr::operator bool() is declared explicit. This is generally a good thing to do, but unfortunately it breaks code that relies on implicit conversions, such as this function (which is the cause of your error):

virtual bool has_dst() const
{
  return (dst_calc_rules_); //if calc_rule is set the tz has dst
}

where dst_calc_rules_ is a shared_ptr.

Until someone at Boost gets round to fixing it, there are two things you could do:

  • Hack that function to return bool(dst_calc_rules_);
  • Define BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS to allow implicit conversions.
like image 182
Mike Seymour Avatar answered Nov 03 '22 22:11

Mike Seymour