在查看boost asio ssl_client.cpp example并在顶部找到了这个:

enum { max_length = 1024 };

不知道,这和
namespace {
    const int max_length = 1024;
}

或者
static const int max_length = 1024;

也许它们是绝对相等的,但这只是更短?

最佳答案

如果您将其用作值,则它们是等效的,而不是通过引用。
enum { constantname = initializer };惯用法曾经在头文件中非常流行,因此您可以在类声明中使用它而不会出现问题:

struct X {
    enum { id = 1 };
};

因为使用静态const成员,您将需要类外初始化程序,并且该初始化程序不能位于头文件中。

更新

这些天很酷的 child 这样做:
struct X {
    static constexpr int id = 1;
};

或者他们与ScottMeyer¹一起写:
struct X {
    static const int id = 1;
};

// later, in a cpp-file near you:
const int X::id;

int main() {
    int const* v = &X::id; // can use address!
}

¹参见Declaration-only integral static const and constexpr datamembers, Item #30

关于c++ - 编写静态const uint变量和匿名枚举变量有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33652582/

10-11 23:23
查看更多