Does template typedefs set a static initialization order?

Within the same compilation unit, the C ++ standard says that the static order of initialization is well defined - this is the order of declarations of static objects. But using the Sun Studio 12 compiler, I come across unintuitive behavior. I defined a templated template helper<T>that contains a static _datatype member Tand a static member function that uses what is _datacalled foo. In my .cpp file, I have this above main ():

struct A { /* some definition */ };

typedef helper<int> s0;
typedef helper<A> s1;

Note that typedef for helper<int>matches up to typedef for helper<A>. So, according to the standard, I would expect it to helper<int>::_databe built before helper<A>::_data(remember, that _datais a static member). On the GCC, this is so; on the Sun, it is not.

This is problematic because the constructor uses helper<int>::_data. I have only one compilation unit that does not have an earlier potential instance helper<A>, so I thought the order should be clearly defined. Is this a Sun compiler bug, or is typedef not technically a definition / specification? I mean, is Sun compiler behavior a valid standard?

I have the following main ():

int main()
{
    //Swapping the order of these has no effect on Sun
    s0::foo();
    s1::foo();
}

There are no other uses for s0 or s1.

+3
2

++ , - .

. typedef-name. - . , :

typedef, helper<int> , , .

, helper<int>. , (, helper<int> , helper<int>::... - ).

. . - .

struct C { C() { printf("hey\n"); } };
struct A { 
  static C a;
  static C b;
};

C A::b;
C A::a;

b , a b.

2 1:

struct C { C(int n) { printf("%d\n", n); } };

template<int N>
struct A {
  static C c;
};

template<int N>
C A<N>::c(N);

// explicit instantiation of declaration and definition
template struct A<2>;
template struct A<1>;

int main() {

}

, main.

struct C { C(int n) { printf("%d\n", n); } };

template<int N>
struct A {
  static C c;
};

template<int N>
C A<N>::c(N);

// implicit instantiation of declarations
A<2> a2;
A<1> a1;

int main() {
  // A<1>::c; A<2>::c;
}

, . , . 14.6.4.1 " ":

, - - , , [... ]. , .

main. , . - khow (GCC 1 2, main swapped, prints 2 1), , .

. .

+6

.

:

s0 one;
s1 two;

.

s0?

typedefs s0; , .

0

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


All Articles