Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, why would you want that? I don't see any reason, or advantage:</p> <p>Anyway, you can do this:</p> <pre><code>template&lt;typename T&gt; int size(const std::vector&lt;T&gt; &amp;v) { return (int) v.size(); } //use std::vector&lt;int&gt; ints; //... int count = size(ints); </code></pre> <p>Still I don't see any point in doing that. You can simply write:</p> <pre><code>int count = (int) ints.size(); </code></pre> <p>But I would still say its not better than the following :</p> <pre><code>size_t count = ints.size(); //don't prefer anything over this. Always use size_t </code></pre> <h3>Advice: avoid using <code>int</code> for size. Prefer <code>size_t</code>.</h3> <hr> <p>As for the edit in your question. Why don't you use <code>size_t</code> as:</p> <pre><code>size_t something = 3; if(arr.size() &gt; something) </code></pre> <p>No warning. In my opinion, if you choose the data type consistently throughout your program, you wouldn't come across a situation when you've to compare <code>int</code> with <code>size_t</code> which is defined as <em>unsigned</em> integral type.</p> <p>Or if there is some legacy code which you've to work with, and which use <code>int</code> for size, then I think its better to use explicit cast when you need it, instead of adding a function in the framework itself, which hides the potential problem:</p> <pre><code>int something = /*some legacy code API call or something */; if(arr.size() &gt; (size_t) something) //or even better; size_t something = (size_t) /*some legacy code API call or something */; if(arr.size() &gt; something) </code></pre>
    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. 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