开发者

About strongly typed enumerations forward declaration

I've a set of classes in my namespace and want to group forward declaration in a define.hpp file.

I've already achieve this in others projects, here is an example :

namespace Makefile
{
    class Builder;
    class Config;
    class Options;
    class Target;
    class Tool;
}

My problem is that — for example — the Config class include the following strongly typed enum :

namespace Makefile
{
    class Config
    {   
    public:
    enum class OperatingSystem : unsigned int 
        {   
            MacOSX = 0,
            Linux = 1,
            Windows = 2 
        };  
    };
}

So I would like to add this enum class in my forward-declaration file, and add the following line :

enum class Config::OperatingSystem : unsigned int;

Here comes my problem, I get the following error when compiling :

src/Makefile/define.hpp:6:13: error: opaque-enum-specifier must use a simple identifier

Does it mean that开发者_如何学JAVA it is not possible to forward-declare nested type? Is there any workaround to this problem? Is this due to my compiler?

My configuration : GCC 4.6.0 on Mac OS X.7

Compilation flags : --std=c++0x -g3 -gdwarf-2 -W -Wall -iquote gen -iquote src


You simply make the opaque enum in the usual place:

namespace Makefile
{
    class Config
    {   
    public:
    enum class OperatingSystem : unsigned int;
    };
}

And then, wherever you want to define the enum constants:

namespace Makefile
{
    enum Config::OperatingSystem : unsigned int
    {   
        MacOSX = 0,
        Linux = 1,
        Windows = 2 
    };
}

The error opaque-enum-specifier must use a simple identifier is there to tell you that if you need to refer to the enum with other than a simple identifier, you are doing it wrong.

UPDATE: For your specific needs, you may simply declare the enum outside of the class, or else in a simple class with only the opaque enum, and inherit Config from it:

namespace Makefile
{
    class ConfigDecl
    {
     public:
        enum class OperatingSystem : unsigned int;
    };
    class Config : public ConfigDecl;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜