开发者

Constants in C/C++

开发者 https://www.devze.com 2023-01-20 01:50 出处:网络
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 def开发者

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 def开发者_JS百科ine constants, but I've never seen any others. Are there any others?


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.


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.,


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

0

精彩评论

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