Compile-Time Constant Expected for Template Class With "static const" Identifier
<p>The goal is to write a meta program that determines whether or not a class has any virtual functions, a fairly easy task. My code is below.</p>

<pre><code>template &lt;typename T&gt;
class HasVirtual :
    public T
{
public:
    static const bool has_virtual;
    virtual void doit() {}
};

template &lt;typename T&gt;
bool const HasVirtual&lt;T&gt;::has_virtual = sizeof(T) == sizeof(HasVirtual&lt;T&gt;);
</code></pre>

<p>The issue I'm running into is that when I try and use this elsewhere, like so:</p>

<pre><code>template &lt;typename T, bool isVirtual = HasVirtual&lt;T&gt;::has_virtual&gt;
class PointerCopy
{
public:
    static T *copy(const T *x)
    {
        return new T(*x);
    }
};

template &lt;typename T&gt;
class PointerCopy&lt;T, true&gt;
{
public:
    static T *copy(const T *x)
    {
        return x-&gt;copy();
    }
};
</code></pre>

<p>I get the error <code>error C2975: 'isVirtual' : invalid template argument for 'PointerCopy', expected compile-time constant expression</code> even though <code>has_virtual</code> is defined with the <code>static const</code> identifier. I ended up "cheating" and defining another class like so:</p>

<pre><code>template &lt;typename T&gt;
class VirtualDerived :
    public T
{
public:
    virtual void fun() {}
};

template &lt;typename T&gt;
class HasVirtual
{
public:
    static const bool has_virtual = sizeof(T) == sizeof(VirtualDerived&lt;T&gt;);
};
</code></pre>

<p>Which works perfectly. Still, I would like to know why I was getting this issue (or if this is an issue with the Visual Studio 2012 compiler). This seems kind of silly to be legitimate: defining a constant outside of the class shouldn't make it no longer a constant.</p>
