Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot specify a type restriction in the template type argument. However, generally, you don't have to.</p> <p>If you simply do:</p> <pre><code>template &lt;typename T&gt; addTri( T v1, T v2, T v3 ) { Vertex &amp;v1r = v1; // .... } </code></pre> <p>This will work if the function is instantiated with a derivative of Vertex. It will create an (obscure) error if <code>T &amp;</code> is <em>not</em> convertible to <code>Vertex &amp;</code>.</p> <p>If you don't even care if the types are convertible to <code>Vertex</code> as long as they have the same members, you can even skip the assignment - C++ template arguments essentially work using <a href="http://en.wikipedia.org/wiki/Duck_typing" rel="noreferrer">duck typing</a>; if you do <code>v1.x</code>, and <code>T</code> contains a member named <code>x</code>, then it will work, whatever type <code>T</code> might actually be.</p> <p>You can be a bit more sophisticated using boost's <a href="http://www.boost.org/doc/libs/1_47_0/libs/type_traits/doc/html/index.html" rel="noreferrer">type-traits library</a> and a <a href="http://www.boost.org/doc/libs/1_47_0/doc/html/boost_staticassert.html" rel="noreferrer">static assertion</a>; with this, you can start defining an assertion to make the error a bit easier to understand:</p> <pre><code>template &lt;typename T&gt; addTri( T v1, T v2, T v3 ) { BOOST_STATIC_ASSERT_MSG(boost::is_convertible&lt;T&amp;, Vertex&amp;&gt;::value, "Template argument must be a subclass of Vertex"); Vertex &amp;v1r = v1; // .... } </code></pre>
    singulars
    1. This table or related slice is empty.
    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