Developer Blog - Inventic.eu
  • Skipper - The ORM Designer
  • VsBuilds - Parallel building
  • Pulpo - Free Skipper CLI

Tag: #templates

C++ - unpack sub-type from template structure only for specified object type

#include &lt;assert.h&gt;<br />
#include &lt;typeinfo&gt;</p>
<p>struct CTreeObject1<br />
{<br />
};</p>
<p>/************************************************************************/<br />
/* Nammed structs - for using one type more than once in SinglesCont.   */<br />
/************************************************************************/<br />
template&lt; class TObject, class TName&gt;<br />
struct NammedObject : public TObject, public TName<br />
{<br />
        typedef TObject object_type;<br />
};</p>
<p>struct stFirst{};<br />
struct stSecond{};</p>
<p>template &lt;typename T&gt;<br />
struct UnpackObjectType<br />
{<br />
        typedef T TT;<br />
};</p>
<p>template&lt;typename T, typename U&gt;<br />
struct UnpackObjectType&lt; NammedObject&lt;T, U&gt; &gt;<br />
{<br />
        typedef T TT;<br />
};</p>
<p>int main()<br />
{<br />
        typedef NammedObject&lt;CTreeObject1,stFirst&gt;  T1;<br />
        typedef NammedObject&lt;CTreeObject1,stSecond&gt;  T2;<br />
        typedef CTreeObject1                        T3;</p>
<p>        UnpackObjectType&lt;T1&gt;::TT d1;<br />
        UnpackObjectType&lt;T2&gt;::TT d2;<br />
        UnpackObjectType&lt;T3&gt;::TT d3;</p>
<p>        //required<br />
        assert( typeid(d1) == typeid(d2));<br />
        assert( typeid(d2) == typeid(d3));</p>
<p>        return 0;<br />
}<br />
10 Jun 2011

Posted by: ludek.vodicka

Programming #C++ #templates

g++ error: expected primary-expression before > token

For g++ compiler is sometimes necessary to specify more typename / template keywords than for Visual Studio. This is one of examples.

To the following code it's necessary to add template keyword:

TMemoryManager &amp; memMngr = GetMemoryManager();<br />
m_pObject =<br />
  memMngr.CreateObject&lt;TObject&gt;(this, m_pContainerOwner);<br />

use

TMemoryManager &amp; memMngr = GetMemoryManager();<br />
m_pObject =<br />
  memMngr.template CreateObject&lt;TObject&gt;(this, m_pContainerOwner);<br />

07 Jun 2011

Posted by: ludek.vodicka

Programming Linux #Linux #C++ #g++ #templates