title: constexpr initializing static member using static function
body: <h1>Requirements</h1>
  
  <p>I want a <code>constexpr</code> value (i.e. a compile-time constant) computed from a <code>constexpr</code> function. And I want both of these scoped to the namespace of a class, i.e. a static method and a static member of the class.</p>
  
  <h1>First attempt</h1>
  
  <p>I first wrote this the (to me) obvious way:</p>
  
  <pre><code>class C1 {
    constexpr static int foo(int x) { return x + 1; }
    constexpr static int bar = foo(sizeof(int));
  };
  </code></pre>
  
  <p><code>g++-4.5.3 -std=gnu++0x</code> says to that:</p>
  
  <pre><code>error: ‘static int C1::foo(int)’ cannot appear in a constant-expression
  error: a function call cannot appear in a constant-expression
  </code></pre>
  
  <p><code>g++-4.6.3 -std=gnu++0x</code> complains:</p>
  
  <pre><code>error: field initializer is not constant
  </code></pre>
  
  <h1>Second attempt</h1>
  
  <p>OK, I thought, perhaps I have to move things out of the class body. So I tried the following:</p>
  
  <pre><code>class C2 {
    constexpr static int foo(int x) { return x + 1; }
    constexpr static int bar;
  };
  constexpr int C2::bar = C2::foo(sizeof(int));
  </code></pre>
  
  <p><code>g++-4.5.3</code> will compile that without complaints. Unfortunately, my other code uses some range-based <code>for</code> loops, so I have to have at least 4.6. Now that I look closer at the <a href="http://gcc.gnu.org/projects/cxx0x.html" rel="noreferrer">support list</a>, it appears that <code>constexpr</code> would require 4.6 as well. And with <code>g++-4.6.3</code> I get</p>
  
  <pre><code>3:24: error: constexpr static data member ‘bar’ must have an initializer
  5:19: error: redeclaration ‘C2::bar’ differs in ‘constexpr’
  3:24: error: from previous declaration ‘C2::bar’
  5:19: error: ‘C2::bar’ declared ‘constexpr’ outside its class
  5:19: error: declaration of ‘const int C2::bar’ outside of class is not definition [-fpermissive]
  </code></pre>
  
  <p>This sounds really strange to me. How do things “differ in <code>constexpr</code>” here? I don't feel like adding <code>-fpermissive</code> as I prefer my other code to be rigurously checked. Moving the <code>foo</code> implementation outside the class body had no visible effect.</p>
  
  <h1>Expected answers</h1>
  
  <p>Can someone explain what is going on here? How can I achieve what I'm attempting to do? I'm mainly interested in answers of the following kinds:</p>
  
  <ul>
  <li>A way to make this work in gcc-4.6</li>
  <li>An observation that later gcc versions can deal with one of the versions correctly</li>
  <li>A pointer to the spec according to which at least one of my constructs <em>should</em> work, so that I can bug the gcc developers about actually getting it to work</li>
  <li>Information that what I want is impossible according to the specs, preferrably with some insigt as to the rationale behind this restriction</li>
  </ul>
  
  <p>Other useful answers are welcome as well, but perhaps won't be accepted as easily.</p>