Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The type expected by <code>multiset</code> second parameter is a <em>Predicate</em>. That is the type of an object <code>c</code> such that <code>c(lhs, rhs)</code> returns something <em>convertible to <code>bool</code></em>.</p> <p>The type of <code>compare</code> and <code>decltype(compare)</code> is <code>bool(shared_ptr&lt;int&gt;, shared_ptr&lt;int&gt;)</code>, a <em>function type</em>. There can't be values of <em>function type</em>, but there can be values of <em>pointers</em> and <em>references</em> to <em>function types</em>. That's why you need the <code>*</code> there. (Actually, there are values of <em>function types</em> and are those things that you declare as functions, but those cannot be created, copied, nor pretty much anything besides calling them and getting their address.) Summing up:</p> <pre><code>decltype(compare)* ==&gt; bool (*)(shared_ptr&lt;int&gt;, shared_ptr&lt;int&gt;) </code></pre> <p>which is a pointer to a function taking two <code>shared_ptr&lt;int&gt;</code>s by value and returning a <code>bool</code>. Then, in the <code>multiset</code> constructor, you give it a value of that type:</p> <pre><code>items{ compare }; // value </code></pre> <p>or more specifically, a value of a <em>function type</em> that will <em>decay</em> to a function pointer type. So let's make that explicit:</p> <pre><code>items{ &amp;compare }; // pointer to function compare </code></pre> <p>If you didn't give it anything, then it would be a <em>null pointer</em>, and when trying to call it to do the comparison your application would crash.</p> <p>In summary, you chose a hell of a complicated case to go through (and I have skipped a few things I should mention). This is a simpler approach:</p> <pre><code>struct shared_ptr_compare { bool operator()(std::shared_ptr&lt;int&gt; const&amp; lhs, std::shared_ptr&lt;int&gt; const&amp; rhs) const { return /*...*/; } }; multiset&lt;shared_ptr&lt;int&gt;, shared_ptr_compare&gt; item; // no need to initialize // shared_ptr_compare with anything, a default value will do just fine </code></pre> <p><strong>Note</strong>: I dropped the top-level <code>const</code> modifier as it has no effect on a function parameter, it only signals the function body that the values cannot be changed. So it does have meaning, but complicates explaining types.</p> <p><strong>Note 2</strong>: You should take those parameters as <code>shared_ptr&lt;int&gt; const&amp;</code> (same thing as <code>const shared_ptr&lt;int&gt;&amp;</code>), since you do not actually need a copy and those copies have to keep a reference counter which is an operation that has to be synchronized among threads.</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