c++ - Making unordered initialization statc ordered -


the following class uses crtp attempt add types std::vector has schwarz counter ensuring initialization order. according 3.6.2/2 member h_ has unordered initialization. how change ensure has ordered initialization? deriving class have nothing more correctly inherit class.

#ifndef p_h_ #define p_h_  #include "pr.h"  template <typename t> class p {    class helper    {    public:       helper()       {          pr.push_back(typeid(t));       }    };    static helper h_; };  template <typename t> typename p<t>::helper p<t>::h_;  #endif 

the standard pattern type of problem use generator instead of exposing global static variable. (this old problem in c++)

so change:

static helper h_ ; 

to:

static helper & h_() ; 

and define this:

template <typename t> typename p<t>::helper & p<t>::h_() {   static p<t>::helper value_ ;   return value_ ; } 

thus guaranteed initialized before used.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -