6

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?

1 Answer 1

12

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.
Sign up to request clarification or add additional context in comments.

2 Comments

In the 1.53 release notes: Smart pointers now use explicit operator bool on C++11 compilers. This can break code that passes a smart pointer to a function taking a bool, or that returns a smart pointer from a function with a bool return type. Please use p != 0 or !!p in such cases.
It's still not fixed in 1.54, but there is a nice patch for that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.