<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Using type names from a template class without full qualifiers?]]></title><description><![CDATA[<p>Hello.</p>
<p>I have a lot of useful typedefs which need to be accessible from classes of my program (I have many classes, each class requires nearly all typedefs). I can make this, for example, in such way:</p>
<pre><code class="language-cpp">class MyTypes
{
public:
  typedef vector&lt; vector&lt;int&gt; &gt; mySuperType;
  typedef vector&lt; vector&lt; vector&lt;int&gt; &gt; &gt; myUltraType;
  //And so on...
};

class MyClass: public MyTypes
{ //All types from MyTypes are accessible in MyClass:
  mySuperType x;
};
</code></pre>
<p>Of course, I can also use namespace for this. That is not a problem.</p>
<p>Recently my program became more complicated: nearly all of my types (defined in <code>MyTypes</code> ) must be templates. But typedef templates are illegal. Hopefully, all these templates depend on the same types, so I decided to convert the whole <code>MyTypes</code> into template:</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyTypes
{
public:
  typedef vector&lt; vector&lt;C&gt; &gt; mySuperType;
  typedef vector&lt; vector&lt; vector&lt;C&gt; &gt; &gt; myUltraType;
  //And so on...
};
</code></pre>
<p>The problem is that old-good way of importing all types into a class is not working (the error is described here: <a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-256174-and-highlight-is-.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-256174-and-highlight-is-.html</a> ):</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass: public MyTypes&lt;C&gt;
{
  mySuperType x; //Error here. I do not want to write &quot;typename MyTypes&lt;C&gt;::mySuperType&quot; EVERYWHERE
};
</code></pre>
<p>I tried to use namespace template for this, but without any success. It looks like my compiler does not support namepace templates <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<pre><code class="language-cpp">template&lt;class C&gt; namespace MyTypes //Error here.
{
  typedef vector&lt; vector&lt;C&gt; &gt; mySuperType;
  typedef vector&lt; vector&lt; vector&lt;C&gt; &gt; &gt; myUltraType;
  //And so on...
};

template&lt;class C&gt; class MyClass
{
  using namespace MyTypes&lt;C&gt;
  mySuperType x;
};
</code></pre>
<p>Is there any way to import all types from a template to a class declaration for them to be used without full qualifiers?</p>
<p>The only way I found is to import types one-by-one:</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass
{
  typedef MyTypes&lt;C&gt;::mySuperType mySuperType;
  typedef MyTypes&lt;C&gt;::myUltraType myUltraType;
  //And so on....  
  mySuperType x;
};
</code></pre>
<p>Any suggestions? May be the whole program design is bad?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/259945/using-type-names-from-a-template-class-without-full-qualifiers</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 15:48:11 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/259945.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 29 Jan 2010 19:29:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Using type names from a template class without full qualifiers? on Fri, 29 Jan 2010 19:33:35 GMT]]></title><description><![CDATA[<p>Hello.</p>
<p>I have a lot of useful typedefs which need to be accessible from classes of my program (I have many classes, each class requires nearly all typedefs). I can make this, for example, in such way:</p>
<pre><code class="language-cpp">class MyTypes
{
public:
  typedef vector&lt; vector&lt;int&gt; &gt; mySuperType;
  typedef vector&lt; vector&lt; vector&lt;int&gt; &gt; &gt; myUltraType;
  //And so on...
};

class MyClass: public MyTypes
{ //All types from MyTypes are accessible in MyClass:
  mySuperType x;
};
</code></pre>
<p>Of course, I can also use namespace for this. That is not a problem.</p>
<p>Recently my program became more complicated: nearly all of my types (defined in <code>MyTypes</code> ) must be templates. But typedef templates are illegal. Hopefully, all these templates depend on the same types, so I decided to convert the whole <code>MyTypes</code> into template:</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyTypes
{
public:
  typedef vector&lt; vector&lt;C&gt; &gt; mySuperType;
  typedef vector&lt; vector&lt; vector&lt;C&gt; &gt; &gt; myUltraType;
  //And so on...
};
</code></pre>
<p>The problem is that old-good way of importing all types into a class is not working (the error is described here: <a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-256174-and-highlight-is-.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-256174-and-highlight-is-.html</a> ):</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass: public MyTypes&lt;C&gt;
{
  mySuperType x; //Error here. I do not want to write &quot;typename MyTypes&lt;C&gt;::mySuperType&quot; EVERYWHERE
};
</code></pre>
<p>I tried to use namespace template for this, but without any success. It looks like my compiler does not support namepace templates <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<pre><code class="language-cpp">template&lt;class C&gt; namespace MyTypes //Error here.
{
  typedef vector&lt; vector&lt;C&gt; &gt; mySuperType;
  typedef vector&lt; vector&lt; vector&lt;C&gt; &gt; &gt; myUltraType;
  //And so on...
};

template&lt;class C&gt; class MyClass
{
  using namespace MyTypes&lt;C&gt;
  mySuperType x;
};
</code></pre>
<p>Is there any way to import all types from a template to a class declaration for them to be used without full qualifiers?</p>
<p>The only way I found is to import types one-by-one:</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass
{
  typedef MyTypes&lt;C&gt;::mySuperType mySuperType;
  typedef MyTypes&lt;C&gt;::myUltraType myUltraType;
  //And so on....  
  mySuperType x;
};
</code></pre>
<p>Any suggestions? May be the whole program design is bad?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847202</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847202</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Fri, 29 Jan 2010 19:33:35 GMT</pubDate></item><item><title><![CDATA[Reply to Using type names from a template class without full qualifiers? on Fri, 29 Jan 2010 19:35:57 GMT]]></title><description><![CDATA[<p>imo there is no way</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass
{
  typedef MyTypes&lt;C&gt;::mySuperType mySuperType;
  typedef MyTypes&lt;C&gt;::myUltraType myUltraType;
  //And so on....  
  mySuperType x;
};
</code></pre>
<p>but this is not the shortest/easiest way:</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass
{
  using MyTypes&lt;C&gt;::mySuperType;
  using MyTypes&lt;C&gt;::myUltraType;

  //And so on....  
  mySuperType x;
};
</code></pre>
<p>maybe there is a need for writing <code>typename</code> - i am not sure...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847204</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847204</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 29 Jan 2010 19:35:57 GMT</pubDate></item><item><title><![CDATA[Reply to Using type names from a template class without full qualifiers? on Fri, 29 Jan 2010 19:45:31 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass
{
  using MyTypes&lt;C&gt;::mySuperType;
  using MyTypes&lt;C&gt;::myUltraType;
</code></pre>
</blockquote>
<p>This is legal but doesn't declare those ids to be types, no help there.</p>
<p>Without enumerating all ids that you wish to &quot;import&quot;, the compiler doesn't know about them, because it cannot possibly look them up until the actual template parameter is known. Therefore there can't be a better way than through typedefs.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847210</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847210</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 29 Jan 2010 19:45:31 GMT</pubDate></item><item><title><![CDATA[Reply to Using type names from a template class without full qualifiers? on Fri, 29 Jan 2010 20:25:19 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass
{
  using MyTypes&lt;C&gt;::mySuperType;
  using MyTypes&lt;C&gt;::myUltraType;
</code></pre>
</blockquote>
<p>This is legal but doesn't declare those ids to be types, no help there.</p>
</blockquote>
<p>This is interesting. I played with this code and found that identifiers <code>mySuperType</code> and <code>myUltraType</code> are usable as type names if <code>MyTypes</code> is defined as a base class of <code>MyClass</code> :</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass: public MyTypes&lt;C&gt;
{
  using MyTypes&lt;C&gt;::mySuperType; //Visual C++ magically knows that &quot;mySuperType&quot; is a type
  using MyTypes&lt;C&gt;::myUltraType;
  mySuperType x;
};
</code></pre>
<p>I think this is non-conformant behaviour of my Visual C++ compiler. But will the <code>typename</code> keyword help? May be this code will solve a problem on GCC compiler:</p>
<pre><code class="language-cpp">template&lt;class C&gt; class MyClass: public MyTypes&lt;C&gt;
{
  using typename MyTypes&lt;C&gt;::mySuperType; //notice &quot;typename&quot; here
  using typename MyTypes&lt;C&gt;::myUltraType;
  mySuperType x;
};
</code></pre>
<p>?</p>
<p>P.S. I like the last variant because I can <code>#define</code> long text to be short:</p>
<pre><code class="language-cpp">#define using_type using typename MyTypes&lt;C&gt;::

template&lt;class C&gt; class MyClass: public MyTypes&lt;C&gt;
{
  using_type mySuperType;
  using_type myUltraType;
  mySuperType x;
};
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847219</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847219</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Fri, 29 Jan 2010 20:25:19 GMT</pubDate></item><item><title><![CDATA[Reply to Using type names from a template class without full qualifiers? on Fri, 29 Jan 2010 22:11:24 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
</blockquote>
<p>Using a macro, I would rather implement it like this:</p>
<pre><code class="language-cpp">// Important: The typename keyword is even at typedefs required
#define USING_TYPE(TYPE) typedef typename MyTypes&lt;C&gt;::TYPE TYPE

template &lt;class C&gt;
class MyClass : public MyTypes&lt;C&gt;
{
    USING_TYPE(mySuperType);
    USING_TYPE(myUltraType);
    // ...
};

#undef USING_TYPE
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1847269</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847269</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 29 Jan 2010 22:11:24 GMT</pubDate></item><item><title><![CDATA[Reply to Using type names from a template class without full qualifiers? on Fri, 29 Jan 2010 23:24:21 GMT]]></title><description><![CDATA[<p>Hm.<br />
It seriously sucks that one can't use &quot;using namespace&quot; inside a class definition.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1847326</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1847326</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 29 Jan 2010 23:24:21 GMT</pubDate></item></channel></rss>