【问题标题】:correct way to refer enum items of `boost::date_time`引用`boost::date_time`的枚举项的正确方法
【发布时间】:2012-04-11 15:28:26
【问题描述】:
#include "boost/date_time/gregorian/gregorian.hpp"

int main()
{
    boost::gregorian::greg_weekday dWeek(boost::date_time::Wednesday);

    //Code One
    // warning C4482: nonstandard extension used: enum 'boost::date_time::weekdays' used in qualified name
    if (dWeek.as_enum()==boost::gregorian::greg_weekday::weekday_enum::Wednesday)
    {
        std::cout << "Today is Wednesday" << std::endl;
    }

    //class BOOST_DATE_TIME_DECL greg_weekday : public greg_weekday_rep {
    //public:
    //    typedef boost::date_time::weekdays weekday_enum;

    //Code Two
    if (dWeek.as_enum()==boost::date_time::Wednesday)
    {
        std::cout << "Today is Wednesday" << std::endl;
    }
}

问题> 我见过大量使用Code One 来比较boost::date_time 的代码。基于 C++ 标准,枚举的使用是不正确的。我提供了一个代码二的解决方案。

谁能给我快速看看,看看它是否是正确的比较方法?

谢谢

【问题讨论】:

    标签: c++ boost boost-date-time


    【解决方案1】:

    编辑:更正

    使用

    boost::date_time::Wednesday
    

    我没有查看 as_enum() 返回的类型。修复它,编译和工作(在 MSVC2k10,Boost 1.48.0 自建)

    EDIT2:您会发现它隐藏在 boost/date_time/gregorian/greg_facet.hpp 中。

    namespace boost{
    namespace gregorian{
      typedef boost::date_time::weekdays weekday_enum;
    }
    }
    

    无论如何,其余信息的相关部分是有一个 boost::date_time::weekdays::Wednesday,但我们去掉了工作日。

    枚举基本上是这样的:

    enum foo { bar = 1, barre = 2, barred = 3 };
    // Is sort-of the same as
    typedef static const int foo;
    foo bar = 1;
    foo barre = 2;
    foo barred = 3;
    

    foo 不是命名空间,也不是结构或类似的东西,它更像是一个类型名。

    我知道它完全不一样,但它也可能是为了使用它们。在这样的类型中使用 weekday_enum 限定符基本上会给编译器带来一点点垃圾来解析,就像说:

    typedef int foo;
    struct S {
      static foo var;
    } p;
    
    p.foo::var = 4; // Does this make sense? Nope...
    

    【讨论】:

    • 没有。 boost::gregorian::greg_weekday::Wednesday 导致错误 C2039: 'Wednesday' : is not a member of 'boost::gregorian::greg_weekday'
    • @q0987 抱歉,我的错,已修复。我没有检查 as_enum() 返回的类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多