S
subramanian100in
Consider the program x.cpp:
#include <cstdlib>
#include <string>
using namespace std;
class Test
{
static string str;
};
string Test::str;
int main()
{
return EXIT_SUCCESS;
}
In the above program, consider the initialization
string Test::str;
The above initialization belongs to dynamic initialization category.
As per the ISO/IEC-14882:2003 document in section "3.6.2
Initialization of non-local objects" paragraph 3:
“It is implementation-defined whether or not the dynamic
initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace
scope is done before the first statement of main. If the
initialization is deferred to some point in time after the first
statement of main, it shall occur before the first use of any
function or object defined in the same translation unit as the object
to be initialized.”
As per the above paragraph, whether the dynamic initialization of
Test::str is done before the first statement of main is IMPLEMENTATION-
DEFINED. Am I correct ?
But Bjarne Stroustrup, in his book "The C++ Programming Language -
Third Edition" (NOT the Special Third Edition) mentions the following
in page 252 in TC++PL in
section "10.4.9 Nonlocal Store":
"A variable defined outside any function (that is, global, namespace,
and class static variables) is initialized (constructed) before main()
is invoked"
So, as per the above statement of Stroustrup, the class static data
member "Test::str" should be initialized before main() is invoked,
WITH ALL COMPILERS ie WITH ALL IMPLEMENTATIONS. Is this understanding
of mine is correct ? If I am correct, then there seems to be a
disagreement between the above statement in Stroustrup's book in page
252 in section "10.4.9 Nonlocal Store" and the ISO/IEC-14882:2003
document in section "3.6.2 Initialization of non-local objects"
paragraph 3. Am I correct in this? If I am wrong kindly explain where
I have misunderstood.
Thanks
V.Subramanian
#include <cstdlib>
#include <string>
using namespace std;
class Test
{
static string str;
};
string Test::str;
int main()
{
return EXIT_SUCCESS;
}
In the above program, consider the initialization
string Test::str;
The above initialization belongs to dynamic initialization category.
As per the ISO/IEC-14882:2003 document in section "3.6.2
Initialization of non-local objects" paragraph 3:
“It is implementation-defined whether or not the dynamic
initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace
scope is done before the first statement of main. If the
initialization is deferred to some point in time after the first
statement of main, it shall occur before the first use of any
function or object defined in the same translation unit as the object
to be initialized.”
As per the above paragraph, whether the dynamic initialization of
Test::str is done before the first statement of main is IMPLEMENTATION-
DEFINED. Am I correct ?
But Bjarne Stroustrup, in his book "The C++ Programming Language -
Third Edition" (NOT the Special Third Edition) mentions the following
in page 252 in TC++PL in
section "10.4.9 Nonlocal Store":
"A variable defined outside any function (that is, global, namespace,
and class static variables) is initialized (constructed) before main()
is invoked"
So, as per the above statement of Stroustrup, the class static data
member "Test::str" should be initialized before main() is invoked,
WITH ALL COMPILERS ie WITH ALL IMPLEMENTATIONS. Is this understanding
of mine is correct ? If I am correct, then there seems to be a
disagreement between the above statement in Stroustrup's book in page
252 in section "10.4.9 Nonlocal Store" and the ISO/IEC-14882:2003
document in section "3.6.2 Initialization of non-local objects"
paragraph 3. Am I correct in this? If I am wrong kindly explain where
I have misunderstood.
Thanks
V.Subramanian