Note that there are some explanatory texts on larger screens.

plurals
  1. POCompare boost::any contents
    text
    copied!<p>I am using a container to hold a list of pointers to anything:</p> <pre><code>struct Example { std::vector&lt;boost::any&gt; elements; } </code></pre> <p>To insert elements in this container, I had written a couple of helper functions (members of the <code>struct Example</code>):</p> <pre><code>void add_any(boost::any&amp; a) { elements.push_back(a); } template&lt;typename T&gt; void add_to_list(T&amp; a) { boost::any bany = &amp;a; add_any(bany); } </code></pre> <p>Now, I would like to insert elements only when they are not present in this container. To do this, I thought that I would only need to call <code>search</code> over <code>elements</code> with an appropriate comparator function. However, I do not know how to compare the <code>boost::any</code> instances.</p> <p><strong>My question:</strong> Knowing that my <code>boost::any</code> instances always contain a pointer to something; is it possible to compare two <code>boost::any</code> values?</p> <hr> <p><strong>update</strong></p> <p>I thank you for your answers. I have also managed to do this in a <em>probably</em> unsafe way: using <code>boost::unsafe_any_cast</code> to obtain a <code>void**</code> and comparing the underlying pointer.</p> <p>For the moment, this is working fine. I would, however, appreciate your comments: maybe this is a big mistake!</p> <pre><code>#include &lt;boost/any.hpp&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; using namespace std; bool any_compare(const boost::any&amp; a1, const boost::any&amp; a2) { cout &lt;&lt; "compare " &lt;&lt; *boost::unsafe_any_cast&lt;void*&gt;(&amp;a1) &lt;&lt; " with: " &lt;&lt; *boost::unsafe_any_cast&lt;void*&gt;(&amp;a2); return (*boost::unsafe_any_cast&lt;void*&gt;(&amp;a1)) == (*boost::unsafe_any_cast&lt;void*&gt;(&amp;a2)); } struct A {}; class Example { public: Example() : elements(0), m_1(3.14), m_2(42), m_3("hello"), m_4() {}; virtual ~Example() {}; void test_insert() { add_to_list(m_1); add_to_list(m_2); add_to_list(m_3); add_to_list(m_4); add_to_list(m_1); // should not insert add_to_list(m_2); // should not insert add_to_list(m_3); // should not insert add_to_list(m_4); // should not insert }; template &lt;typename T&gt; void add_to_list(T&amp; a) { boost::any bany = &amp;a; add_any(bany); } private: vector&lt;boost::any&gt; elements; double m_1; int m_2; string m_3; A m_4; void add_any(const boost::any&amp; a) { cout &lt;&lt; "Trying to insert " &lt;&lt; (*boost::unsafe_any_cast&lt;void*&gt;(&amp;a)) &lt;&lt; endl; vector&lt;boost::any&gt;::const_iterator it; for (it = elements.begin(); it != elements.end(); ++it) { if ( any_compare(a,*it) ) { cout &lt;&lt; " : not inserting, already in list" &lt;&lt; endl; return; } cout &lt;&lt; endl; } cout &lt;&lt; "Inserting " &lt;&lt; (*boost::unsafe_any_cast&lt;void*&gt;(&amp;a)) &lt;&lt; endl; elements.push_back(a); }; }; int main(int argc, char *argv[]) { Example ex; ex.test_insert(); unsigned char c; ex.add_to_list(c); ex.add_to_list(c); // should not insert return 0; } </code></pre>
 

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