【问题标题】:Boost 1.53 local date time compiler error with -std=c++0x使用 -std=c++0x 提升 1.53 本地日期时间编译器错误
【发布时间】:2013-03-05 21:31:35
【问题描述】:

使用 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;
}

使用 -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

如果我不使用 c++0x 选项,一切都很好。 谁能告诉我这是怎么回事?

【问题讨论】:

    标签: c++ boost c++11 g++


    【解决方案1】:

    当您为 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
    }
    

    其中dst_calc_rules_shared_ptr

    在 Boost 的某个人开始修复它之前,您可以做两件事:

    • 将该函数破解为return bool(dst_calc_rules_);
    • 定义 BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS 以允许隐式转换。

    【讨论】:

    • 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.
    • 它在 1.54 中仍未修复,但有一个不错的patch
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2010-10-11
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多