Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Compound types can be constructed using the declarator syntax - found in <code>[dcl.decl]</code>.</p> <p>Underlying this syntax are six fundamental constructs, within which any <code>T</code> can be substituted by any of the other constructs in the list. [In the following, <code>(T)</code> represents a list of zero or more types (which may end in '...'), and <code>&lt;T&gt;</code> represents a list of one or more types.]</p> <pre><code>T // T T* // pointer to T T&amp; // reference to T T[n] // array of size 'n' of T T C::* // pointer to C::member of type T T (T) // function taking '(T)' and returning T </code></pre> <p><strong>EDIT</strong>: The type of a class template specialization can be subsituted for any <code>T</code>:</p> <pre><code>C&lt;T&gt; // specialization of class template C with arguments '&lt;T&gt;' </code></pre> <p>There are combinations of the above that produce constructs which have special significance:</p> <pre><code>T (*)(T) // pointer to function taking '(T)' and returning T T (C::*)(T) // pointer to C::member-function taking '(T)' and returning T </code></pre> <p>Additionally, some of the constructs may be cv-qualified:</p> <pre><code>const T // const T T* const // const pointer to T T C::* const // const pointer to C::member of type T </code></pre> <p>Not all of the combinations result in valid types. According to <code>[basic.compound]</code>, only the following combinations can be used:</p> <blockquote> <p>Compound types can be constructed in the following ways:</p> <ul> <li>arrays of objects of a given type</li> <li>functions, which have parameters of given types and return void or references or objects of a given type</li> <li>pointers to void or objects or functions (including static members of classes) of a given type </li> <li>references to objects or functions of a given type</li> <li>pointers to non-static class members, which identify members of a given type within objects of a given class</li> </ul> </blockquote> <p>Additional restrictions are mentioned:</p> <blockquote> <p><code>[dcl.ptr]</code> there are no pointers to references</p> <p><code>[dcl.ref]</code> There shall be no references to references, no arrays of references, and no pointers to references</p> <p><code>[dcl.mptr]</code> A pointer to member shall not point to ... a member with reference type, or "cv void."</p> <p><code>[dcl.fct]</code> The parameter list (void) is equivalent to the empty parameter list. Except for this special case, void shall not be a parameter type. ... If the type of a parameter includes a type of the form “pointer to array of unknown bound of T” or “reference to array of unknown bound of T,” the program is ill-formed. Functions shall not have a return type of type array or function.</p> </blockquote> <p>Some of the possible constructs <strong>cannot</strong> be used as template-arguments. When you explicitly specify a set of template-arguments, the compiler must check that the template-arguments can be substituted for the template-parameters without resulting in an 'invalid type'. According to <code>[temp.deduct]\2</code>, the following constructs constitute invalid types:</p> <blockquote> <p>Type deduction may fail for the following reasons:</p> <ul> <li><p>Attempting to create an array with an element type that is void, a function type, or a reference type, or attempting to create an array with a size that is zero or negative.</p> <pre><code>template &lt;class T&gt; int f(T[5]); int I = f&lt;int&gt;(0); int j = f&lt;void&gt;(0); // invalid array </code></pre></li> <li><p>Attempting to use a type that is not a class type in a qualified name.</p> <pre><code>template &lt;class T&gt; int f(typename T::B*); int i = f&lt;int&gt;(0); </code></pre></li> <li><p>Attempting to use a type in the qualifier portion of a qualified name that names a type when that type does not contain the specified member, or if the specified member is not a type where a type is required.</p> <pre><code>template &lt;class T&gt; int f(typename T::B*); struct A {}; struct C { int B; }; int i = f&lt;A&gt;(0); int j = f&lt;C&gt;(0); </code></pre></li> <li><p>Attempting to create a pointer to reference type.</p></li> <li><p>Attempting to create a reference to a reference type or a reference to void.</p></li> <li><p>Attempting to create "pointer to member of T" when T is not a class type.</p> <pre><code>template &lt;class T&gt; int f(int T::*); int i = f&lt;int&gt;(0); </code></pre></li> <li><p>Attempting to perform an invalid conversion in either a template argument expression, or an expression used in the function declaration.</p> <pre><code>template &lt;class T, T*&gt; int f(int); int i2 = f&lt;int,1&gt;(0); // can’t conv 1 to int* </code></pre></li> <li><p>Attempting to create a function type in which a parameter has a type of void.</p></li> <li>Attempting to create a cv-qualified function type.</li> </ul> </blockquote> <p><strong>EDIT:</strong> This is based on C++03, but also applies to C++11 (which adds rvalue reference types)</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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