C++枚举重新定义和 C++0x

发布于 2024-11-28 14:33:57 字数 334 浏览 0 评论 0原文

如果我想排除 C++ 中枚举和类型重定义的问题,我可以使用以下代码:

struct VertexType
{
    enum
    {
        Vector2 = 1,
        Vertor3 = 2,
        Vector4 = 3,
    };
};

struct Vector2 { ... };
struct Vector3 { ... };
struct Vector3 { ... };

Is there a way to remove thewrapper above enum.我查看了 C++0x 但没有找到有关解决此问题的附加信息。

If I want to exclude problems with enums and type redefinition in C++ I can use the code:

struct VertexType
{
    enum
    {
        Vector2 = 1,
        Vertor3 = 2,
        Vector4 = 3,
    };
};

struct Vector2 { ... };
struct Vector3 { ... };
struct Vector3 { ... };

Is there a way to remove the wrapper above enum. I looked at C++0x but didn't find addiditional inforamtion about solving this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

錯遇了你 2024-12-05 14:33:57

由于您正在谈论 C++0x,因此只需使用新的 enum class 语法:

enum class VertexType {
   Vector1 = 1,
   Vector2 = 2,
   Vector4 = 3
};

枚举器值只能通过 VertexType 类型访问,如 VertexType 中所示: :矢量1。

该标准的一些引用:

§7.2/2 [...] enum-keys 枚举类和枚举结构在语义上是等效的;使用其中之一声明的枚举类型是作用域枚举,并且其枚举器是作用域枚举器。 [...]

§7.2/10 [...]每个作用域枚举器都在枚举范围内声明。[...]

// example in §7.2/10
enum class altitude { high=’h’, low=’l’ };
void h() {
  altitude a;        // OK
  a = high;          // error: high not in scope
  a = altitude::low; // OK
}

Since you are talking about C++0x, just use the new enum class syntax:

enum class VertexType {
   Vector1 = 1,
   Vector2 = 2,
   Vector4 = 3
};

The enumerator values will only be accessible through the VertexType type as in VertexType::Vector1.

Some quotes from the standard:

§7.2/2 [...] The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators. [...]

§7.2/10 [...] Each scoped enumerator is declared in the scope of the enumeration.[...]

// example in §7.2/10
enum class altitude { high=’h’, low=’l’ };
void h() {
  altitude a;        // OK
  a = high;          // error: high not in scope
  a = altitude::low; // OK
}
噩梦成真你也成魔 2024-12-05 14:33:57

命名空间怎么样?

namespace VertexType
{
    enum V
    {
        Vector2 = 1,
        Vertor3 = 2,
        Vector4 = 3,
    };
}

struct Vector2 { ... };
struct Vector3 { ... };
struct Vector4 { ... };

How about namespace?

namespace VertexType
{
    enum V
    {
        Vector2 = 1,
        Vertor3 = 2,
        Vector4 = 3,
    };
}

struct Vector2 { ... };
struct Vector3 { ... };
struct Vector4 { ... };
心意如水 2024-12-05 14:33:57

看来 vector3 已经被使用了。你可以做你想做的事,但是,不能使用vector3。

enum //VertexType
{
    Vector2 = 1,
    //Vector3 = 2,
    Vector4 = 3,
};

struct Vector2 { ... };
//struct Vector3 {  };
struct Vector3 { ... };

这对我有用,完全没有错误。

这是我找到的一个链接。
http://www.kixor.net/dev/vector3/

It appears vector3 is already being used. You can do what your trying to do, however, vector3 cannot be used.

enum //VertexType
{
    Vector2 = 1,
    //Vector3 = 2,
    Vector4 = 3,
};

struct Vector2 { ... };
//struct Vector3 {  };
struct Vector3 { ... };

This works for me, no errors at all.

This is a link i found.
http://www.kixor.net/dev/vector3/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文