Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot directly provide it, but you can actually use <code>any</code> as the underlying type... though for pointers it's pointless (ah!)</p> <pre><code>struct any { std::type_info const&amp; _info; void* _address; }; </code></pre> <p>And a templated constructor:</p> <pre><code>template &lt;typename T&gt; any::any(T* t): _info(typeid(*t)), _address(dynamic_cast&lt;void*&gt;(t)) { } </code></pre> <p>This is, basically, <code>boost::any</code>.</p> <p>Now we need to "augment" it with our comparison mechanism.</p> <p>In order to do so, we'll "capture" the implementation of <code>std::less</code>.</p> <pre><code>typedef bool (*Comparer)(void*,void*); template &lt;typename T&gt; bool compare(void* lhs, void* rhs) const { return std::less&lt;T&gt;()(*reinterpret_cast&lt;T*&gt;(lhs), *reinterpret_cast&lt;T*&gt;(rhs)); } template &lt;typename T&gt; Comparer make_comparer(T*) { return compare&lt;T&gt;; } </code></pre> <p>And augment the constructor of <code>any</code>.</p> <pre><code>struct any { std::type_info const&amp; _info; void* _address; Comparer _comparer; }; template &lt;typename T&gt; any::any(T* t): _info(typeid(*t)), _address(dynamic_cast&lt;void*&gt;(t)), _comparer(make_comparer(t)) { } </code></pre> <p>Then, we provided a specialization of <code>less</code> (or <code>operator&lt;</code>)</p> <pre><code>bool operator&lt;(any const&amp; lhs, any const&amp; rhs) { if (lhs._info.before(rhs._info)) { return true; } if (rhs._info.before(lhs._info)) { return false; } return (*lhs._comparer)(lhs._address, rhs._address); } </code></pre> <p><em>Note: encapsulation, etc... are left as an exercise to the reader</em></p>
    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. 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.
    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