Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat are the expression syntax over types C++ support?
    primarykey
    data
    text
    <p>I was working with a templated class which takes a set of integers. The code was like,</p> <pre><code>template&lt;unsigned... Idx&gt; struct work{ ... }; </code></pre> <p>Then I realized, user may need to provide either a set of integers, or a range of integers. So, I changed the syntax little to support instantiation like,</p> <pre><code>work&lt;indexes&lt;1,3,2,4&gt; &gt; //instead of work&lt;1,3,2,4&gt; work&lt;span&lt;1,4&gt; &gt; //same as work&lt;1,2,3,4&gt; </code></pre> <p>While, in C++ we have large number of operators, and can be used to formulate exotic expression templates (say <code>boost::xpressive</code>, <code>boost::lambda</code>, <code>boost::spirit</code> etc) , possibilities for type manipulation is much less.</p> <p>In a boostcon keynote by Sean Parent, he noted one still can not write <code>pair&lt;int&gt;</code> to denote <code>a pair of integers</code>. In my persinal library, I made a syntax like <code>tuple&lt;int[3]&gt;</code> to denote a tuple of 3 integers, instead of writing a tuple with 3 int in the type arguments, noting that I do not write a raw array as tuple argument anywhere! (note: <code>std::array&lt;int,3&gt;</code> is not same as the above, as std::array can not store references while <code>tuple</code> can, say <code>std::tuple&lt;int&amp;,int&amp;,int&amp;&gt;</code> is possible)</p> <p>So, I want to know what are the different kind of "type expressions" I can write? </p> <p>So far I can think of object type, function type, reference type, with/without cv modifiers, pointers etc. e.g</p> <pre><code> template&lt;class T&gt; struct tpl; using t1 = tpl&lt;int&gt;;//simple type //function (can have function pointer/reference also) // e.g. void(*)(int,float) or void(&amp;)(int,float) using t2 = tpl&lt;void(int,float)&gt;; //array can have pointer / reference also //e.g. int(&amp;)[4] or int (*)[4] using t3 = tpl&lt;int[4]&gt;; using t4 = tpl&lt;int[]&gt;; using t5 = tpl&lt;int const&gt;;//with cv modifiers using t6 = tpl&lt;int*&gt;;//with pointer using t7 = tpl&lt;int&amp;&gt;;//with reference (&amp; or &amp;&amp;) using t8 = tpl&lt;tpl&lt;int&gt; &gt;; //template itself using t9 = tpl&lt;void(...)&gt;; //variadic functions using t10 = tpl&lt;R C::*&gt;; //pointer to member </code></pre> <p>But I believe, many more are possible.</p> <p>NOTE: This question is purely theoretical, <strong>I just want to know all kinds of syntax I can write inside &lt;> as type argument</strong>, and not about the readability/morality aspect of it, or even how can I implement some of the examples I had given, like the work class. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload