正确的方法来引用`boost :: date_time`的枚举项

时间:2012-04-11 15:28:27

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

#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;
    }
}

问题&GT;我已经看到大量使用 Code One 进行boost::date_time比较的代码。基于C ++标准,枚举的用法不正确。我提供的解决方案为 Code Two

有人可以快速查看一下,看看它是否真的是正确的比较方式吗?

谢谢

1 个答案:

答案 0 :(得分: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...