Note that there are some explanatory texts on larger screens.

plurals
  1. POLazy evaluation on templated function parameter
    text
    copied!<p>I have a class for logging, which MUST NOT inherit std::ostream, and has operator&lt;&lt; defined for the same types as the standard output stream PLUS a templated version:</p> <pre><code>class MyLoggingClass { [...] public: template&lt;typename T&gt; MyLoggingClass &amp; operator&lt;&lt;( T&amp; data ){ ... } } </code></pre> <p>Also, for every printable class in my program, I have the typical non-member function defined:</p> <pre><code>std::ostream &amp; operator &lt;&lt; ( std::ostream &amp; os, const OneOfMyClasses &amp; foo ); </code></pre> <p>The thing is that, internally, my logger sometimes uses an standard output stream, this is:</p> <pre><code>template&lt;typename T&gt; MyLoggingClass &amp; operator&lt;&lt;( T&amp; data ) { [...] if( someCondition ) { cout &lt;&lt; data; } [...] } </code></pre> <p>Thanks to this, I can log my classes without explicitly replicating on every of them the non-member operator&lt;&lt; for MyLoggingClass. The problem comes in a line where I try to log an object created "on the fly":</p> <pre><code>MyLoggingClass logger; logger &lt;&lt; OneOfMyClasses(params); // Here I am invoking the constructor of class "OneOfMyClasses" </code></pre> <p>The thing is that, instead of invoking the constructor, and then passing the object as parameter of the operator&lt;&lt;, it interprets that I'm trying to log a pointer to function.</p> <p>Of course, some valid solutions for this problem include:</p> <ul> <li>Removing the templated operator&lt;&lt;, and making MyLoggingClass extend std::ostream</li> <li>Removing the templated operator&lt;&lt;, and creating tons of non-member operator&lt;&lt;( MyLoggingClass &amp;, const OneOfMyClasses &amp; )</li> <li>Storing the object to log in a temporal variable, and then doing "logger &lt;&lt; temporalObject;" However, I would like to know if there is a way to force the compiler evaluating the constructor call. Do you know any workaround for this case?</li> </ul> <p>Thank you in advance for your time :)</p>
 

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