Portál AbcLinuxu, 9. května 2025 05:50
static const int pole[5];
To pole bych chtěl nainizializovat. To samozřejmě umím třeba takto:
const int XXX::pole[] = { 1, 2, 3, 4, 5 }
Teď přijde ta zajímavější část. Mám také statickou metodu, třeba suma:
static int suma(const int *pole, int N){ sectu N prvku v pole a vratim vysledek; }
A chtěl bych bych ji použít v inicializaci tak, že parametrem bude právě inicializované pole, tedy třeba takto:
const int XXX::pole[] = { 1, 2, 3, 4, XXX::suma(XXX::pole,4) }
Mám normou zaručeno, že to bude fungovat vždy? Tedy říká něco norma o pořadí inicializovaných prvků pole? Bude se vždy volat metoda suma
až naposled? S g++ mi to funguje, překladač se nebrání, ani warning žádný a hodnota je také správná. Chtěl bych ale vědět, zda je to podle normy.
Prosím, nepište mi odpovědi typu "dá se to obejít například takto: xxxx", případně "takovou hovadinu přece nikdo nemůže potřebovat". Znám dost způsobů, jak se takovéto inicializaci vyhnout, chtěl bych ale znát odpověd na můj dotaz.
Ono je to trochu slozitejsi.
3.6.2 1
Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (3.9) with static storage duration initialized with constant expressions (5.19) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit. [Note: 8.5.1 describes the order in which aggregate members are initialized. The initialization of local static objects is described in 6.7. ]
8.5.1 14
When an aggregate with static storage duration is initialized with a brace-enclosed initializer-list, if all the member initializer expressions are constant expressions, and the aggregate is a POD type, the initialization shall be done during the static phase of initialization (3.6.2); otherwise, it is unspecified whether the initialization of members with constant expressions takes place during the static phase or during the dynamic phase of initialization.
#include <iostream>
static int suma(const int *pole, int N)
{
int ret = 0;
do {
std::cout << N << ':' << pole[N] << std::endl;
ret += pole[N];
} while (N--);
return ret;
}
const int pole[] = { suma(pole, 4), 1, 2, 3, 4 };
int main()
{
std::cout << pole[0] << std::endl;
return 0;
}
$ g++ test.cc && ./a.out 4:4 3:3 2:2 1:1 0:0 10
Koukám, že v otázce je to ještě buď v namespace nebo ve struktuře. Mimo strukturu se to chová tak, jak jsem uvedl, je to v 3.6.2:
Objects with static storage duration shall be zero-initialized before any other initialization takes place.
To je ten první prvek, kde je volána ta funkce suma; před volám funkce suma je inicializován na nulu
Objects of POD types with static storage duration initialized with constant expressions shall be initialized before any dynamic initialization takes place.
To jsou ty konstantní hodnoty, jsou inicializovány ještě předtím, než se zavolá ta funkce suma
Nicméně ve struktuře to není definováno:
[...]; otherwise [struktura není POD nebo inicializace není provedena konstantami], it is unspecified whether the initialization of members with constant expressions takes place during the static phase or during the dynamic phase of initialization
To jste nasel kde? Ja ty citace ctu tak, ze v pripade, kdy se inicializuje konstantnimi i nekonstatnimi vyrazy, tak vyslovne neni specifikovano, jestli se inicializace konstantami ma delat pred dynamickou fazi, nebo behem ni.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.