|
From: Stephan Brönnimann on 15 Jan 2006 14:40 Given the following sample code: datetime.h ========== class Date { public: // Constructor with number of days since 01-Jan-1900. explicit Date(numDays) : numDays_(numDays) {} // minimum supported date static const Date& minDate(); private: // number of days since 01-Jan-1900 long numDays_; }; datetime.cc =========== #include "datetime.h" namespace { Date minimumDate(-10000); }; const Date& Date::minDate() { return minimumDate; } Does the standard guarantee that minimumDate is always initialized if Date::minDate() is called from the initialization of other static variables (global or at file scope) in different compilation units? Note: I'm not looking for a different implementation of Date::minDate(), e.g., using a static Date inside the function. This would not be thread-safe because the real (relevant) constructor computes Date::numDays_ from the day, month and year. Regards, Stephan broeni(a)osb-systems.com Open source rating and billing engine for communication networks. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mirek Fidler on 16 Jan 2006 09:16 > datetime.h > ========== > class Date { > public: > // Constructor with number of days since 01-Jan-1900. > explicit Date(numDays) : numDays_(numDays) {} > > // minimum supported date > static const Date& minDate(); > private: > // number of days since 01-Jan-1900 > long numDays_; > }; > > datetime.cc > =========== > #include "datetime.h" > > namespace { > Date minimumDate(-10000); > }; > > const Date& Date::minDate() > { > return minimumDate; > } > > Does the standard guarantee that minimumDate is always initialized if > Date::minDate() is called from the initialization of other static > variables (global or at file scope) in different compilation units? No. > Note: I'm not looking for a different implementation of > Date::minDate(), e.g., using a static Date inside the function. This > would not be thread-safe because the real (relevant) constructor Yes, that is corrent, at least for most platforms (I believe that latest GCC has thread-safe static variables). Means you have to find a way around for threading safety. Possible solution would be to use some global POD variable (like pointer) that is guaranteed to be zeroed at startup and bind thread-locked stuff around it. Anyway, AFAIK, there is no 100% portable solution here. Mirek [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Tutone on 16 Jan 2006 09:17 Stephan Br?nnimann wrote: > Given the following sample code: > > datetime.h > ========== > class Date { > public: > // Constructor with number of days since 01-Jan-1900. > explicit Date(numDays) : numDays_(numDays) {} > > // minimum supported date > static const Date& minDate(); > private: > // number of days since 01-Jan-1900 > long numDays_; > }; > > datetime.cc > =========== > #include "datetime.h" > > namespace { > Date minimumDate(-10000); > }; I believe that last semi-colon above is extraneous. > const Date& Date::minDate() > { > return minimumDate; > } > > Does the standard guarantee that minimumDate is always initialized if > Date::minDate() is called from the initialization of other static > variables (global or at file scope) in different compilation units? No. Best regards, Tom [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Alberto Ganesh Barbati on 16 Jan 2006 09:17 Stephan Br?nnimann wrote: > Given the following sample code: > > datetime.h > ========== > class Date { > public: > // Constructor with number of days since 01-Jan-1900. > explicit Date(numDays) : numDays_(numDays) {} > > // minimum supported date > static const Date& minDate(); > private: > // number of days since 01-Jan-1900 > long numDays_; > }; > > datetime.cc > =========== > #include "datetime.h" > > namespace { > Date minimumDate(-10000); > }; > > const Date& Date::minDate() > { > return minimumDate; > } > > Does the standard guarantee that minimumDate is always initialized if > Date::minDate() is called from the initialization of other static > variables (global or at file scope) in different compilation units? Unfortunately, no. > Note: I'm not looking for a different implementation of > Date::minDate(), e.g., using a static Date inside the function. This > would not be thread-safe because the real (relevant) constructor > computes Date::numDays_ from the day, month and year. Well... Thread safety may be an issue only if you are creating threads before entering function main(). If you don't, most probably the implementation will execute all initializations of static objects in the main thread, so the most common implementation: const Date& Date::minDate() { static Date minimumDate(-10000); return minimumDate; } won't be a problem if it's called before entering main(). To avoid multiple initializations *after* entering function main(), just ensure that's it's called before main() (doh!) o at least before creating any thread. HTH, Ganesh [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Maxim Yegorushkin on 16 Jan 2006 11:35
Stephan Br?nnimann wrote: [] > datetime.cc > =========== > #include "datetime.h" > > namespace { > Date minimumDate(-10000); > }; > > const Date& Date::minDate() > { > return minimumDate; > } > > Does the standard guarantee that minimumDate is always initialized if > Date::minDate() is called from the initialization of other static > variables (global or at file scope) in different compilation units? It does not. Use something like schwarz counter idiom to guarantee initialization of minimumDate before its first use. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |