title: Function-Local Static Const variable Initialization semantics
body: <p>The questions are in bold, for those that cannot be bothered reading a question in depth.</p>
  
  <p>This is a followup to <a href="https://stackoverflow.com/questions/2652164/compile-time-meta-programming-with-string-literals">this</a> question. It is to do with the initialization semantics of static variables in functions. Static variables should be initialized once, and their internal state might be altered later - as I (<strong>currently</strong>) do in the linked question. However, the code in question does not require the feature to change the state of the variable later. </p>
  
  <p>Let me clarrify my position, since I don't require the string object's internal state to change. The code is for a  trait class for meta programming, and as such would  would benifit from a const char * const ptr -- thus Ideally  a local cost static const variable is needed. My educated guess is that in this case the string in question will be optimally placed in memory by the link-loader, and that the code is more secure and maps to the intended semantics.</p>
  
  <p>This leads to the semantics of such a variable "The C++ Programming language Third Edition -- Stroustrup" does not have anything (that I could find) to say about this matter. All that is said is that the variable is initialized once when the flow of control of the thread first reaches the code. <strong>This leads me to ponder if the following code would be sensible, and if not what are the intended semantics ?.</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  const char * const GetString(const char * x_in)
  {
      static const char * const x = x_in;
      return x;
  }
  
  int main()
  {
    const char * const temp = GetString("yahoo");
    std::cout &lt;&lt; temp &lt;&lt; std::endl;
    const char * const temp2 = GetString("yahoo2");
    std::cout &lt;&lt; temp2 &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The following compiles on GCC and prints "yahoo" twice. Which is what I want -- However it might not be standards compliant (which is why I post this question). It might be more elegant to have two functions, "SetString" and "String" where the latter forwards to the first. <strong>If it is standards compliant does someone know of a templates implementation in boost (or elsewhere) ?</strong></p>
  
  <p><strong>edit: 11-may-2010</strong> </p>
  
  <p>I am using the following macro to generate the above mentioned getter/setters in my classes that encode compile time information.</p>
  
  <pre><code>#define MACRO_STATIC_SETTING_PTR(name, type)                          \
    static const type const set_##name (const type const name##_in) {   \
      static const type const name = name##_in;                         \
      return name;                                                      \
    }                                                                   \
    static const type const name() {                                    \
      return set_##name(NULL);                                          \
    } 
  
  #define MACRO_STATIC_SETTING(name, type)                              \
    static const type set_##name (const type name##_in) {               \
      static const type name = name##_in;                               \
      return name;                                                      \
    }                                                                   \
    static const type name() {                                          \
      return set_##name(NULL);                                          \
    } 
  </code></pre>
  
  <p>These macros are placed inside a class declaration. For example:</p>
  
  <pre><code>template&lt;class tag&gt;
  class ConfigInstance{
  public:
    MACRO_STATIC_SETTING_PTR(sqllite3_filename, char *)
  };
  </code></pre>
  
  <p>Hopefully this will be useful to others. </p>