title: Is it possible to ensure that a function is only called during the 'static initialization' step
body: <p>I was wondering if it is possible to ensure that a function is only called during the static initialization step of a program?</p>
  
  <p>As an example, say that I have some singleton class that contains a <code>std::map</code> object and exposes the <code>insert</code> and <code>at</code> methods of it. I would like to ensure that reading data from it (the <code>at</code> method) is thread-safe which, to my understanding, requires ensuring that nothing is modifying the data (i.e. using the <code>insert</code> method).</p>
  
  <p>The map is intended to be filled <em>only</em> during static initialization, at which time I'm assuming there is only one thread. Is there any way that I can ensure no misguided user calls <code>insert</code> once <code>main()</code> has begun?</p>
  
  <hr>
  
  <p>Example code</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;string&gt;
  
  class Singleton {
    private:
      std::map&lt;std::string, std::string&gt; m_map;
    public:
      static Singleton&amp; instance() {
        static Singleton theSingleton;
        return theSingleton;
      }
      static bool insert(const std::string&amp; key, const std::string&amp; value) {
        return instance().m_map.insert(std::make_pair(key, value) ).second;
      }
      static std::string at(const std::string&amp; key) {
        return instance().m_map.at(key);
      }
  };
  
  static bool inserted = Singleton::insert("Hello", "World"); // fine
  
  bool addItem(const std::string&amp; key, const std::string&amp; value) {
    return Singleton::insert(key, value); // not OK
  }
  </code></pre>
  
  <hr>
  
  <p>Needless(?) to say the actual code is a good deal more complex than this simple example.</p>
  
  <hr>
  
  <p><strong>Edit after solution:</strong> It looks like the best way to make this as safe as possible is to maintain a <code>status</code> variable that records whether the singleton is in 'insert' or 'read' mode and act accordingly. Thanks to all for their ideas and suggestions!</p>