Static Class Constants

Consider the following snippet:

struct Foo
{
    static const T value = 123; //Where T is some POD-type
};

const T Foo::value; //Is this required?

In this case, does the standard require you to explicitly declare a value in a translation unit? I seem to have conflicting information; and things like numeric_limits from STL seem to do things like in my snippet.

OTOH, I remember reading somewhere (albeit a long time ago) that you still need to provide a declaration in the translation block.

If so, what about specialized specialization? Will each specialization require a declaration?

I would be grateful for what the "right way" is.

0
source share
3 answers

, . , , , .

, , . , " " .

++ Standard . , . , , , .

. use 3.2 One Definition Rule - 9.4.2, paragraph 4 and 5 ( ++ 98. 3 4 n2800 ).

: , ++ 03: , , ( 2003 ), . :

, , (. 5.19), sizeof (5.3.3) typeid, lvalue (5.2.8)...

, , . , , . (. ), ++ 1x , "s == string::npos", , , , 3.2. , , , (?) . .

+3

, litb, n2798:

9.4.2

[...]

2
  , vv.   , .   ,   ::.

+1

, - , - (, ). . Stroustrup ++, 10.4.6.2.

Edit: Unfortunately, I just re-read the question, and the question was for some type T. In general, you will need to give a definition, I agree. But if you used something from the int family, you don’t need to (with a caution above).

0
source

Source: https://habr.com/ru/post/1764771/


All Articles