Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing STL strings that use different allocators
    primarykey
    data
    text
    <p>I'd like to compare STL strings that are allocated with <strong>different allocators</strong>, e.g. an ordinary <code>std::string</code> with a string using a <em>custom STL allocator</em>. Unfortunately, it seems that usual <code>operator==()</code> doesn't work in this case:</p> <pre><code>// Custom STL allocator to allocate char's for string class typedef MyAllocator&lt;char&gt; MyCharAllocator; // Define an instance of this allocator MyCharAllocator myAlloc; // An STL string with custom allocator typedef std::basic_string &lt; char, std::char_traits&lt;char&gt;, MyCharAllocator &gt; CustomAllocString; std::string s1("Hello"); CustomAllocString s2("Hello", myAlloc); if (s1 == s2) // &lt;--- ERROR: doesn't compile ... </code></pre> <p>In particular, MSVC10 (VS2010 SP1) emits the following error message:</p> <blockquote> <p>error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)</p> </blockquote> <p>So, a <em>lower-level</em> (less readable) code like this:</p> <pre><code>if (strcmp(s1.c_str(), s2.c_str()) == 0) ... </code></pre> <p>should be used.</p> <p>(This is also particularly annoying in cases where e.g. there are <code>std::vector</code>'s of differently-allocated strings, where the usual simple <code>v[i] == w[j]</code> syntax can't be used.)</p> <p>This doesn't seem very good to me, since a custom allocator changes the way string memory is requested, but the <em>interface</em> of a string class (including comparison with <code>operator==()</code>) is independent from the particular way a string allocates its memory.</p> <p>Is there something I am missing here? Is it possible to keep the C++ high-level interface and operator overloads in this case?</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.
 

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