jeudi 26 février 2015

C++ initialisation: order of const global vs static class member


Vote count:

0




In the following library code:



#include <cmath>

namespace blah {
const double pi=4.0*std::atan(1.0);
}

template <int I>
class ClassB
{
public:
ClassB() {myval = blah::pi;}
private:
double myval;
};

template <int I>
class ClassA
{
public:
static ClassB<I> b;
};

template<int I>
ClassB<I> ClassA<I>::b = ClassB<I>();

template class ClassA<3>;


is the variable pi guaranteed by the standard to be assigned to its value 4.0*std::atan(1.0) before the constructor of ClassB uses it?


As far as I can tell from the standard, pi and static ClassA<I>::ClassB<I> b should be initialised in the order in which they are defined in this single translation unit - and thus pi should be initialised first.


However, in my real codebase with the code structure as above, I am finding that in code compiled by Clang 3.6, pi is equal to zero at the time that the ClassB constructor is executed, and is initialised to its proper value only after this. (GCC 4.8.3 initialises pi first, as I was expecting.)



asked 1 min ago







C++ initialisation: order of const global vs static class member

Aucun commentaire:

Enregistrer un commentaire