Boost logo

Boost :

Subject: Re: [boost] [filesystem] Version 3 request for comments
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-02-11 11:18:44


----- Original Message -----
From: "Beman Dawes" <bdawes_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, February 11, 2009 4:50 PM
Subject: Re: [boost] [filesystem] Version 3 request for comments

> In C++0x, the preferred practice for such constants is probably going
> to be to use a scoped enum:
>
> enum class file_type
> {
> status_unknown,
> file_not_found,
> regular,
> directory,
> symlink,
> block,
> character,
> fifo,
> socket,
> type_unknown
> };
>
> That's a lot more satisfying. We can emulate that approach in C++03 like this:
>
> #ifndef BOOST_NO_SCOPED_ENUM
>
> enum class file_type
> {
> status_unknown,
> file_not_found,
> regular,
> directory,
> symlink,
> block,
> character,
> fifo,
> socket,
> type_unknown
> };
>
> typedef file_type file_type_enum;
>
> #else
> namespace file_type
> {
> enum types
> {
> status_unknown,
> file_not_found,
> regular,
> directory,
> symlink,
> block,
> character,
> fifo,
> socket,
> type_unknown
> };
> }
>
> typedef file_type::types file_type_enum;
> #endif
>
> Is such a scoped enum approach worth taking?
>
> I haven't made up my mind; I'd like to think about it for a few days
> and also see what could be done to preserve existing code.

Hi,
the question is how the user will use these enumerations. if BOOST_NO_SCOPED_ENUM is not defined the user can write

    file_type e = file_type::file_not_found;

which will not evidently compile when defined. How can we force the user to write

    file_type_enum e = file_type::file_not_found;

which is not intuitive, i.e. there is no explicit relation between file_type_enum and file_type. What about making this relation explicit using a metafunction?

I'm don't know if the following is clearer

    enumeration<file_type>::type e = file_type::file_not_found;

Best,
Vicente
__________________________________
#ifndef BOOST_NO_SCOPED_ENUM
    template <typename E>
    struct enummeration {
        typedef E type;
    };
#else
    template <typename E>
    struct enummeration {
        typedef E::type type;
    };
#endif

#ifndef BOOST_NO_SCOPED_ENUM

enum class file_type
{
    status_unknown,
    file_not_found,
    regular,
    directory,
    symlink,
    block,
    character,
    fifo,
    socket,
    type_unknown
};

typedef file_type file_type_enum;

#else
struct file_type
{
  enum type
  {
    status_unknown,
    file_not_found,
    regular,
    directory,
    symlink,
    block,
    character,
    fifo,
    socket,
    type_unknown
  };
}

typedef file_type::types file_type_enum;
#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk