Is it safe to cast away template arguments in C++?
<p>I'm playing around with inheritance of template arguments in C++. I've got a Child class and a Parent class. The Parent class attempts to cast itself as the Child class. Although this does compile and appears to work, I'd like to know if it's safe to do. Here's the code:</p>

<pre><code>class Empty { };

template&lt;class T&gt; class Child : public T
{
public:
    void do_something() { /* ... */ }
};

class Parent
{
public:
    void go_crazy()
    {
        Child&lt;Empty&gt; &amp; self_as_child = *((Child&lt;Empty&gt; *)this);
        self_as_child.do_something();
    }
}

void main()
{
    Child&lt;Parent&gt; c;
    c.go_crazy();
}
</code></pre>

<p>So, is the self-cast unreliable? It appears to work, but maybe that's just because I got lucky with my particular compiler.</p>
