Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, the code compiles and runs for me in clang++. Can't try with g++ on this computer.</p> <p><strong>Edit:</strong> Actually, it compiles with g++ as well, which makes sense because you only use the <code>operator&lt;&lt;</code> in the main which is in the global namespace. I assume your actual code is different <strong>\Edit</strong></p> <p>But I'm familiar with the "ostream lvalue can't bind to ostream&amp;&amp;" error</p> <p>How to explain. There is a problem in providing <code>operator&lt;&lt;</code> between <code>ostreams</code> and any <code>std</code> class (like <code>list</code> in your example, but I found it with <code>vector</code>)</p> <p>Mostly it works, but when the operator is called from a namespace (like your <code>my</code> namespace) it breaks.</p> <p>Why? Because "where do I look for this operator&lt;&lt; member"? See, there might be a lot of operator&lt;&lt; between ostreams and lists - each in a different namespace. So where does the compiler look for it?</p> <p>It looks in the namespaces of each on its operands (in your case - both are from <code>std</code>). And <em>sometimes</em> in the namespace of the caller (which in your case is <code>my</code>). </p> <p>I say "sometimes" because according to the standard it shouldn't, but g++ does it anyway. clang++ doesn't - but looks in the global namespace instead (hence why it worked for me)</p> <p>Ideally, you'd want to put the operator&lt;&lt; inside the <code>std</code> namespace (try it - it will work). BUT - that is against the standard. You are not allowed to do that. You can put it in the <code>my</code> namespace and it should work find in g++, but not in other compilers.</p> <p>It's a problem. I "solved" it by creating a wrapper - a class that exists in my own namespace and only holds a reference to the <code>std</code> class - and can be printed.</p> <pre><code>template&lt;class T&gt; struct OutList&lt;T&gt;{ const std::list&lt;T&gt; &amp;lst; OutList(const std::list &amp;l):lst(l){} }; template&lt;class T&gt; OutList&lt;T&gt; outlist(const std::list&lt;T&gt; &amp;lst){return OutList&lt;T&gt;(lst);} std::ostream &amp;operator&lt;&lt;(std::stream &amp;out,const OutList&lt;T&gt; &amp;lst){...} .... std::cout &lt;&lt; "list= "&lt;&lt;outlist(list)&lt;&lt;std::endl; </code></pre> <p>It isn't pretty, but that's all I found...</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.
    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.
 

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