Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: insert tuple in multiset with std::function, and keeping an order
    primarykey
    data
    text
    <p>Quite simply, what's wrong with this piece of code?</p> <pre><code> typedef std::function&lt;double()&gt; Event; typedef std::tuple &lt;double, std::function&lt;double()&gt;&gt; Event_handle; std::multiset &lt; Event_handle &gt; event_multiset; std::vector &lt; Event_handle &gt; event_vector; void add_event_handler(double time, Event func_object) { // multiset version gives an error // event_multiset.insert (std::make_tuple(time, func_object)); // vector is ok event_vector.push_back(std::make_tuple(time, func_object)); } </code></pre> <p>compiled using <code>g++ 4.7.2</code> - with command simply <code>g++ -std=c++11 main.cpp</code></p> <p><strong>Why I want to do this?</strong></p> <p>The program is run in real time and the <code>add_even_handler</code> function includes a value of type <code>double</code> called <code>time</code> (note that <code>time</code> variable here has nothing to do with clock or actual time, it is just an increasing object of type double). Therefore when a user add some event it will be called at a certain time.</p> <p>A multiset container under the standard would collate the objects in some order (usually, if not always, <code>std::less&lt;T&gt;</code>). Then looping over the container, I could call the <code>Event</code> upon increasing changes of variable <code>double time</code>.</p> <p><strong>What is the problem?</strong></p> <p>As KyleC has pointed out (see his answer), <code>std::function&lt;&gt;</code> is not understood by the compiler in what process to order</p> <p><strong>How I've overcome the problem</strong></p> <p>You learn something new everyday. The above code was a result of overthinking the problem by initially mixing <code>std::multiset</code> and <code>std::tuple</code>. <code>std::map&lt;T,S&gt;</code> or <code>std::multimap&lt;T,S&gt;</code> is also sorted by an associated <code>key</code> which in this case is of type <code>double</code>, which by default by the standard is again <code>std::less&lt;T&gt;</code>. So instead of the above, I've done something similar to the following</p> <pre><code>std::multimap &lt;double, event&gt; event_map; void add_event_handler(double time, Event func_object) { // multimap is ok event_map.insert(std::make_pair(time,func_object)); } </code></pre> <p>This is just written here in the event it may help people, albeit obvious but nonetheless.</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.
 

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