From 22b934673f7fc8a697c4e5dce5184e95e74a844c Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 1 May 2018 16:59:15 +0200 Subject: [PATCH] Investigation: init order of static template member fields indicates rather questionable behaviour. The standard demands a templated static field to be defined before first odr-use. IIRC, it even demands a static field to be initialised prior to use in a ctor. But here the definition of the templated static member field is dropped off even after the definition of another static field, which uses the (templated) Front-end-class in its initialiser. --- research/try.cpp | 102 ++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 45 deletions(-) diff --git a/research/try.cpp b/research/try.cpp index a034e6bdf..dbad5c04a 100644 --- a/research/try.cpp +++ b/research/try.cpp @@ -37,24 +37,17 @@ // 11/17 - detect generic lambda // 12/17 - investigate SFINAE failure. Reason was indirect use while in template instantiation // 03/18 - Dependency Injection / Singleton initialisation / double checked locking +// 04/18 - investigate construction of static template members /** @file try.cpp - ** Rework of the template lib::Depend for singleton and service access. - ** The (now for the third time rewritten) dependency factory can be configured to yield - ** a subclass singleton, or to bind to an external service. Lazy initialisation relies on - ** Double Checked Locking, which we need switch to C++11 Atomics in order to be correct - ** (in theory). The impact of this initialisation guard should be investigated by benchmark. + * Investigation: static initialisation order -- especially of static template member fields */ typedef unsigned int uint; #include "lib/format-cout.hpp" -#include "lib/depend.hpp" -#include "lib/depend-inject.hpp" -//#include "lib/meta/util.hpp" #include "lib/test/test-helper.hpp" -#include "lib/util.hpp" @@ -64,54 +57,73 @@ typedef unsigned int uint; cout << "Probe " << STRINGIFY(_XX_) << " ? = " << _XX_ < +class Factory { - volatile int theHole_ = rand() % 1000; + public: + T val; - public: - int readMe() { return theHole_; } + Factory() + : val{} + { + cout << "Factory-ctor val="< +class Front + { + public: + static Factory fac; + + Front() + { + cout << "Front-ctor val="< mystery; + cout << "make-Front..."< fint; - thread_local int64_t cnt = 0; + int& i = fint.operate(); + cout << "main: val="<