When overriding the << (stream) operator, debug goes into std's << impl instead?
<p>This question has been partially answered: the solution is that in one of my class's fields, specifically _escape, has UB. However, it is still unknown why Visual Studio's stack trace does not display the operator&lt;&lt; function nor does the debugger find it - almost as if there was an optimization(I disabled optimizations) to remove the symbol and merge it with the print function. </p>

<h1>What I already know</h1>

<p>I have seen all the popular posts on SO about &lt;&lt; operator overload. I have followed all the basic semantics. This isn't a duplicate question of any of the popular posts.</p>

<p>To override the &lt;&lt; operator, one needs to define a function with the general format of:</p>

<pre><code>ostream&amp; operator&lt;&lt;(ostream&amp; os, const MyObject&amp; dt)
</code></pre>

<p>I have done so in my program,</p>

<p>I'm using Microsoft Visual Studio 2015 for this project, and using the debugger to try to step into the &lt;&lt; operator function. However, it never gets inside of the function that I defined as follows:</p>

<pre><code>std::ostream&amp; operator &lt;&lt;(std::ostream&amp; os, const EscapeStr&amp; t)
{
    t.print(os);
    return os;
}
</code></pre>

<p>where EscapeStr is a class I defined with the following function definition for <code>print</code>:</p>

<pre><code>void print(std::ostream&amp; os) const { 
    for (int i = 0; i &lt; _elem.length(); i++) {
        char c = _elem[i];
        if (_delim[c]) {
            for (int j = 0; j &lt; _escLen; j++) {
                os &lt;&lt; _escape[j];
            }
        }
        os &lt;&lt; _elem[i];
    }
}
</code></pre>

<h1>The problem</h1>

<p>I see the print() function being called here using the VS debugger, but I cannot observe the program entering the scope of my overridden &lt;&lt;, but rather it's going into the std definition of &lt;&lt;, with the following signature:</p>

<pre><code>template&lt;class _Traits&gt; inline
    basic_ostream&lt;char, _Traits&gt;&amp; operator&lt;&lt;(
        basic_ostream&lt;char, _Traits&gt;&amp; _Ostr,
        const char *_Val);  // insert NTBS into char stream
</code></pre>

<p>This is extremely weird, because only the ostream override operator has access to my public function currently. Is Visual Studio just lying?</p>

<p>I don't understand what's going on, as one of my other overloads do work, and the signature isn't even exact(without the const and reference):</p>

<pre><code>/// ostream operator for indent
std::ostream&amp; operator &lt;&lt;(std::ostream &amp;os, indent x)
{
    static const std::string spaces("    ");
    while(x.d--) {
        os &lt;&lt; spaces; 
    }
    return os;
}
</code></pre>

<p>As a result, my program is exhibiting some UB(reading the std library code would drive me insane). Here's an ideone to show the UB: <a href="https://ideone.com/jxro5s" rel="nofollow noreferrer">https://ideone.com/jxro5s</a> . Any help would be greatly appreciated.</p>

<h1>Extra information for MVCE and clarity</h1>

<pre><code>const static char _delims[] = { '\\', '"' };
const static std::vector&lt;char&gt; delims(_delims, _delims + 2);

class EscapeStr {
    const static unsigned short MAX_CHAR = 256;

    std::string &amp;_elem;
    bool _delim[MAX_CHAR];
    const char* _escape;
    int _escLen;
public:
    EscapeStr(std::string &amp;elem,
        const std::vector&lt;char&gt; &amp;delim = std::vector&lt;char&gt;(1, '"'),
        const std::string &amp;escape = "\\") :
        _elem(elem),
        _escape(escape.c_str()),
        _escLen(escape.size())
    {
        for (int i = 0; i &lt; MAX_CHAR; i++) {
            _delim[i] = false;
        }
        for (int i = 0; i &lt; delim.size(); i++) {
            _delim[delim[i]] = true;
        }
    }
    void print(std::ostream&amp; os) const {
        for (int i = 0; i &lt; _elem.length(); i++) {
            char c = _elem[i];
            if (_delim[c]) {
                for (int j = 0; j &lt; _escLen; j++) {
                    os &lt;&lt; _escape[j];
                }
            }
            os &lt;&lt; _elem[i];
        }
    }
};
</code></pre>

<p>Here is an image of the stack trace - no sign of the &lt;&lt; operator.
<a href="https://i.stack.imgur.com/S28o8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S28o8.png" alt="enter image description here"></a></p>

<p>EDIT: To predict future comments/posts about me not using std::quoted - I'm trying to make the program compatible with versions less than C++11.</p>