title: Initializing const member within class declaration in C++
body: <p>In PHP and C# the constants can be initialized as they are declared:</p>
  
  <pre><code>class Calendar3
  {
      const int value1 = 12;
      const double value2 = 0.001;
  }
  </code></pre>
  
  <p>I have the following C++ declaration of a functor which is used with another class to compare two math vectors:</p>
  
  <pre><code>struct equal_vec
  {
      bool operator() (const Vector3D&amp; a, const Vector3D&amp; b) const
      {
          Vector3D dist = b - a;
          return ( dist.length2() &lt;= tolerance );
      }
  
      static const float tolerance = 0.001;
  };
  </code></pre>
  
  <p>This code compiled without problems with g++. Now in C++0x mode (-std=c++0x) the g++ compiler outputs an error message:</p>
  
  <blockquote>
    <p>error: ‘constexpr’ needed for in-class initialization of static data member ‘tolerance’ of non-integral type</p>
  </blockquote>
  
  <p>I know I can define and initialize this <code>static const</code> member outside of the class definition. Also, a non-static constant data member can be initialized in the initializer list of a constructor.</p>
  
  <p>But is there any way to initialize a constant within class declaration just like it is possible in PHP or C#?</p>
  
  <h3>Update</h3>
  
  <p>I used <code>static</code> keyword just because it was possible to initialize such constants within the class declaration in g++. I just need a way to initialize a constant in a class declaration no matter if it declared as <code>static</code> or not.</p>