使用-std = c ++ 0x增加1.53本地日期时间编译器错误

Cra*_*aig 6 c++ boost g++ c++11

使用g ++版本4.7.2,如果我尝试编译以下内容

#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;
}
Run Code Online (Sandbox Code Playgroud)

使用-std = c ++ 0x我收到以下错误.

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
Run Code Online (Sandbox Code Playgroud)

如果我不使用c ++ 0x选项,一切都很好.谁能告诉我这里发生了什么?

Mik*_*our 12

在为C++ 11构建时,boost::shared_ptr::operator bool()声明了explicit.这通常是一件好事,但遗憾的是它会破坏依赖于隐式转换的代码,例如此函数(这是导致错误的原因):

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

这里dst_calc_rules_是一个shared_ptr.

直到Boost的某个人开始修复它,你可以做两件事:

  • 哈哈那个功能 return bool(dst_calc_rules_);
  • 定义BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS以允许隐式转换.

  • 在[1.53发行说明](http://www.boost.org/users/history/version_1_53_0.html)中:`智能指针现在在C++ 11编译器上使用显式运算符bool.这可以破坏将智能指针传递给采用bool的函数的代码,或者从具有bool返回类型的函数返回智能指针的代码.在这种情况下请使用p!= 0或!! p.` (4认同)
  • 因此智能指针的更改会导致日期时间错误吗? (3认同)