title: Cannot use sizeof(member variable) to initialize a static const value in Visual Studio
body: <p>I've been developing a library on a Fedora 23 box using g++ 5.3.1, and successfully building this on a Windows box using mingw (I can fetch the version here if necessary).  I handed this code off to a coworker today interested in using it.  He tried compiling it in Visual Studio 2013 (his IDE of choice), and it blew up.  Below I've created an MCVE of the problem:</p>
  
  <pre><code>#include "stdafx.h"  // include this while in Visual Studio
  #include &lt;iostream&gt;  // include when compiling with g++
  
  class staticTest
  {
  public:
    staticTest() { };
    ~staticTest() { };
  
    unsigned myVal;
  
  private:
    static const size_t staticLength = sizeof(myVal);  // errors in VS2013, compiles fine with g++ and mingw
    const size_t length_ = sizeof(myVal);  // compiles fine for all
  };
  </code></pre>
  
  <p>This compiles without error or warning with <code>g++ -Wall -Wextra -std=c++11 -c staticTest.cpp</code>.  However, in Visual Studio 2013, I get the following 3 errors about the <code>staticLength</code> assignment line:</p>
  
  <pre><code>statictest.cpp(12): error C2327: 'staticTest::myVal' : is not a type name, static, or enumerator
  statictest.cpp(12): error C2065: 'myVal' : undeclared identifier
  statictest.cpp(12): error C2070: 'unknown-type': illegal sizeof operand
  </code></pre>
  
  <p>I know what the errors are basically saying, it doesn't see the member variable in the <code>static</code> context, and therefore cannot apply the <code>sizeof</code> operator to it.  Through clenched teeth, I approved changing all such instances to <code>static const size_t staticLength = sizeof(unsigned);</code> and it built fine after that.  This problem compounded even further in another instance where I tried doing something similar in a <code>static</code> function with <code>sizeof(memberArray)</code> and <code>sizeof(memberArray[0])</code>.</p>
  
  <p>I'm assuming this is standard compliant, since <code>g++</code> likes it and I've heard before Microsoft compilers are loose with the standards, accepting a kind of pseudo-c/c++ language.</p>
  
  <ol>
  <li>Is this bad coding style?  I always prefer using <code>sizeof(myVariable)</code> because it's more maintainable (if the type of <code>myVariable</code> changes, no changes are necessary to the <code>sizeof</code>).  Seems like to me the compiler should know (as g++ does) the type of <code>myVal</code> at compile time and not care whether it's a member of a class or not.</li>
  <li>Any chance if anybody knows of a work-around for this in Visual Studio 2013, or if this problem is fixed in later versions of Visual Studio?  The real kicker for me is Windows mingw compiles this just fine, but my coworker won't budge from VS, so here I am modifying my code, making it less-maintainable, just to comply with the whims of a wonky IDE/compiler.  Oh and his final executable will be built for Linux and run on the same box as mine, so building it in VS isn't even necessary or a requirement... [angry/puking emoji]</li>
  </ol>
  
  <p><strong>EDIT</strong>
  I have also tried:</p>
  
  <pre><code>static const size_t staticLength = sizeof(this-&gt;myVal);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>static const size_t staticLength = sizeof(staticTest::myVal);
  </code></pre>
  
  <p>Both failed.</p>