简体   繁体   English

C / C ++中的常数

[英]Constants in C/C++

How many different ways are there to define constants in C or C++? 在C或C ++中定义常量有几种不同的方法?

I am already aware of using the const keyword and the #define directive. 我已经知道使用const关键字和#define指令。 I heard somewhere that there are two more ways to define constants, but I've never seen any others. 我在某处听说有两种定义常量的方法,但我从未见过其他方法。 Are there any others? 还有其他吗?

enum , as in enum { some_constant= 2 }; enum ,如enum { some_constant= 2 };

EDIT: I also forgot the additions of constexpr and user defined literals in the C++0x standard. 编辑:我也忘记了constexpr和用户定义的文字在C ++ 0x标准中的添加。 So there are actually three additional ways. 因此,实际上还有另外三种方式。

For the basic informal meaning of "constant" you have #define , const and enum , as already mentioned by other answers. 对于“常量”的基本非正式含义,您可以使用# #defineconstenum ,如其他答案所提到的。

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). 首先,我确定您知道宏是邪恶的,因此我不会毫不奇怪:仅对#define说“ no”(作为定义常量的一种方法)。

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. 其次,当您需要用旧的编译器可以移植的代码定义整数类型的常量static成员时, enum就派上用场了。

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 ; 细微的部分是,没有定义,就无法在某处正式使用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 eg double , or a string, whatever. 而且,部分微妙之处在于,除了历史意外之外,没有其他原因,您只能对整数类型执行上述操作,而不能对double类型或字符串进行上述操作。

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. 有效地创建一个类型为double的成员常量,可以使用模板化的常量成语作为一种解决方法。 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. 例如,您可能想利用该技巧在头文件中完全定义类Blah

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. 由于对类模板的特殊支持,它可以工作-因为如果没有该特殊规则,实际上就不可能在头文件中使用static成员常量定义类模板。

Cheers & hth., 干杯,……

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM