简体   繁体   中英

Is the number of static variables limited in C++

I was talking with developpers from a platform which are claiming to support C++, but they said, in the same breath, that the number of static variables that are cleand-up at programm exit is bound to a magic number (i think they said 32 on their platform), they say that is conform with the C++ standard.

Is this correct?

The standard says that all destructors must be called if they have side effects.

[basic.start.term]/1: Destructors (12.4) for initialized objects (that is, objects whose lifetime (3.8) has begun) with static storage duration are called as a result of returning from main and as a result of calling std::exit (18.5). [...]

[basic.stc.static]/2: If a variable with static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 12.8. The keyword static can be used to declare a local variable with static storage duration.

However, the rules on standards compliance do allow for some leeway when it comes to implementation limits:

[intro.compilance]/2.1: If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits , accept and correctly execute that program.

A hard limit on the number of static variables allowed may seem odd, but common implementations have hard limits on all sorts of things, eg template instantiation depth.

If there is a limit in the static variables cleaned-up at program exit, it surely is NOT mandated by the C++ language standard.

Depending on the platform, compiler and runtime library, there may be however be some limitation, ie on embedded platforms with small CPU capabilities and/or strict memory constraints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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