C/C++ 中的常量

发布于 2024-09-27 16:34:35 字数 134 浏览 3 评论 0 原文

在 C 或 C++ 中定义常量有多少种不同的方法?

我已经知道使用 const 关键字和 #define 指令。我在某处听说还有两种定义常量的方法,但我从未见过其他方法。 还有其他人吗?

How many different ways are there to define constants in C or C++?

I am already aware of using the const keyword and the #define directive. I heard somewhere that there are two more ways to define constants, but I've never seen any others.
Are there any others?

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

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

发布评论

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

评论(3

回忆追雨的时光 2024-10-04 16:34:35

enum,如 enum { some_constant= 2 };

编辑:我还忘记了在 C++ 中添加 constexpr 和用户定义的文字0x 标准。所以实际上还有三种额外的方法。

enum, as in enum { some_constant= 2 };

EDIT: I also forgot the additions of constexpr and user defined literals in the C++0x standard. So there are actually three additional ways.

夜司空 2024-10-04 16:34:35

对于“常量”的基本非正式含义,您有#defineconstenum,正如已经提到的其他答案。

但至少在我写这篇文章时,还没有人讨论过如何使用它们。

首先,我确信您知道宏是邪恶的,所以我不会再赘述这一点:只需对 #define (作为定义常量的一种方法)说“不”即可。

其次,当您需要在应该可移植到旧版编译器的代码中定义整数类型的常量静态成员时,枚举会派上用场。

第三,对于“定义”的正式意义来说,有一个可能非常令人惊讶的微妙之处:

struct Blah
{
    static int const x = 42;     // This is just a declaration, not a definition.
};

int const Blah::x;               // This is a definition (yes!).

int main()
{
    // Use Blah::x here.
}

部分微妙之处在于纯粹声明与定义的视觉形式的交换。在几乎任何其他上下文中,带有初始化的声明也是定义。但不适用于这个整型的静态成员!

微妙之处在于,如果没有定义,您就无法在某个地方正式获取 Blah::x 的地址;从形式上讲,正是定义赋予了它存储空间。

而且,微妙之处在于,除了历史意外之外,您只能对整型执行上述操作,而不能对例如 double 或字符串等执行上述操作。

struct Blah
{
    static double const x = 3.14;  // !CAN NOT DO THIS IN C++98.
};

有效创建一个double类型的成员常量,您可以使用模板化常量惯用法作为解决方法。有效地完成编译器的工作,原则上它可以将上面的内容重写为。它是这样的:

template< class Dummy >
struct BlahConstants
{
    static double const x;
};

template< class Dummy >
double const BlahConstants< Dummy >::x = 3.14;

struct Blah
    : BlahConstants< void >
{
    // Whatever.
};

int main()
{
    // Use Blah::x here.
}

例如,您可能希望使用该技巧在头文件中完全定义类 Blah

它的工作原理是由于对类模板的特殊支持——因为如果没有这种特殊规则,实际上不可能在头文件中使用static成员常量定义类模板。

干杯&呵呵,,

For the basic informal meaning of "constant" you have #define, const and enum, as already mentioned by other answers.

But at least as I'm writing this, nobody has yet discussed how to use them.

First, I'm sure you know that Macros Are Evil, so I shall not belabor the point: just say "no" to #define (as a means of defining constants).

Second, the enum comes in handy when you need to define a constant static member of integral type, in code that should be portable to older compilers.

Third, for the formal meaning of "define" there is a perhaps very surprising subtlety:

struct Blah
{
    static int const x = 42;     // This is just a declaration, not a definition.
};

int const Blah::x;               // This is a definition (yes!).

int main()
{
    // Use Blah::x here.
}

Part of the subtlety is the exchange of the visual form of pure declaration versus definition. In just about any other context it's the declaration with initialization that is also the definition. But not for this static member of integral type!

And part of the subtlety is that without the definition, somewhere, you can't formally take the address of Blah::x; it's the definition that, formally, gives it storage.

And, part of the subtlety is that for no good reason other than historical accident, you can only do the above for integral types, not for e.g. double, or a string, whatever.

struct Blah
{
    static double const x = 3.14;  // !CAN NOT DO THIS IN C++98.
};

To effectively create a member constant of, say, type double, you can use the templated constant idiom as a workaround. Effectively doing the compiler's job, what it in principle could have rewritten the above as. It goes like this:

template< class Dummy >
struct BlahConstants
{
    static double const x;
};

template< class Dummy >
double const BlahConstants< Dummy >::x = 3.14;

struct Blah
    : BlahConstants< void >
{
    // Whatever.
};

int main()
{
    // Use Blah::x here.
}

For example, you might want to employ that trick for defining class Blah completely in a header file.

It works due to special support for class templates -- because without that special rule, it would be practically impossible to define class templates with static member constants, in header files.

Cheers & hth.,

凝望流年 2024-10-04 16:34:35

在C中,有数字常量和字符串文字(例如int i = 5,5是一个常量)也许这就是他们的意思。

In C, there are numeric constants and string literals (for example int i = 5, 5 is a constant) Maybe that's what they meant.

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