<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[ostream&amp;lt;&amp;lt; in PIMPL]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich frage mich, wie man in einer PIMPL-Idiom-Klasse den ostream Operator implementiert, wenn man ein Template hat.</p>
<p>Also in der .h:</p>
<pre><code>class A
{
    private:
   struct Impl;
        std::unique_ptr&lt;Impl&gt; pImpl;
};
</code></pre>
<p>Und in der .cpp:</p>
<pre><code>struct Impl
{
int i;
}

A::A()
: pImpl(std::make_unique&lt;Impl&gt;())) 
{
   pImpl-&gt;i = 20;
}
</code></pre>
<pre><code>template &lt;class _OStream&gt;
    inline _OStream&amp; operator&lt;&lt;(_OStream&amp; os, const A&amp; in_a)
    {
        os &lt;&lt; ....  // ???
        return os;
    }
</code></pre>
<p>Wir komme ich an die Member ran, wenn ich in der Header nicht auf diese zugreife (auf das i)?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/330547/ostream-lt-lt-in-pimpl</link><generator>RSS for Node</generator><lastBuildDate>Fri, 03 Jul 2026 02:32:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/330547.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 Jan 2015 16:12:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ostream&amp;lt;&amp;lt; in PIMPL on Thu, 15 Jan 2015 16:12:01 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich frage mich, wie man in einer PIMPL-Idiom-Klasse den ostream Operator implementiert, wenn man ein Template hat.</p>
<p>Also in der .h:</p>
<pre><code>class A
{
    private:
   struct Impl;
        std::unique_ptr&lt;Impl&gt; pImpl;
};
</code></pre>
<p>Und in der .cpp:</p>
<pre><code>struct Impl
{
int i;
}

A::A()
: pImpl(std::make_unique&lt;Impl&gt;())) 
{
   pImpl-&gt;i = 20;
}
</code></pre>
<pre><code>template &lt;class _OStream&gt;
    inline _OStream&amp; operator&lt;&lt;(_OStream&amp; os, const A&amp; in_a)
    {
        os &lt;&lt; ....  // ???
        return os;
    }
</code></pre>
<p>Wir komme ich an die Member ran, wenn ich in der Header nicht auf diese zugreife (auf das i)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2437748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2437748</guid><dc:creator><![CDATA[StefanGG]]></dc:creator><pubDate>Thu, 15 Jan 2015 16:12:01 GMT</pubDate></item><item><title><![CDATA[Reply to ostream&amp;lt;&amp;lt; in PIMPL on Thu, 15 Jan 2015 16:17:42 GMT]]></title><description><![CDATA[<p>Warum ist das ein Tenplate?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2437749</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2437749</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 15 Jan 2015 16:17:42 GMT</pubDate></item><item><title><![CDATA[Reply to ostream&amp;lt;&amp;lt; in PIMPL on Thu, 15 Jan 2015 16:51:43 GMT]]></title><description><![CDATA[<p>manni66 schrieb:</p>
<blockquote>
<p>Warum ist das ein Tenplate?</p>
</blockquote>
<p>Na weil ein std::ostream eigentlich ein std::basic_ostream&lt;char&gt; ist. Also die streams sind Templates.<br />
Man könnte es auch so machen:</p>
<pre><code>template &lt;class CharType&gt;
    inline std::basic_ostream&lt;CharType&gt; Stream&amp; operator&lt;&lt;(std::basic_ostream&lt;CharType&gt;&amp; os, const A&amp; in_a)
    {
        os &lt;&lt; ....  // ???
        return os;
    }
</code></pre>
<p>Aber das löst das eigentliche Problem nicht. Ich fürchte, es geht nicht, ohne die Impl bekannt zu machen. Oder Du implementierst den Operator nur für char und wchar_t. Dann kannst Du die Implementierung wieder in die cpp stecken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2437754</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2437754</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Thu, 15 Jan 2015 16:51:43 GMT</pubDate></item><item><title><![CDATA[Reply to ostream&amp;lt;&amp;lt; in PIMPL on Thu, 15 Jan 2015 17:05:57 GMT]]></title><description><![CDATA[<p>Es braucht sowieso nur die std::ostream-Überladung, alles andere ist unnötig.</p>
<p><a href="http://utf8everywhere.org" rel="nofollow">http://utf8everywhere.org</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2437758</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2437758</guid><dc:creator><![CDATA[wschrott_t]]></dc:creator><pubDate>Thu, 15 Jan 2015 17:05:57 GMT</pubDate></item><item><title><![CDATA[Reply to ostream&amp;lt;&amp;lt; in PIMPL on Thu, 15 Jan 2015 18:05:24 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">friend std::basic_ostream&lt;char&gt; &amp;operator &lt;&lt; (std::basic_ostream&lt;char&gt; &amp;out, A const &amp;a);
friend std::basic_ostream&lt;char32_t&gt; &amp;operator &lt;&lt; (std::basic_ostream&lt;char32_t&gt; &amp;out, A const &amp;a);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2437763</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2437763</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Thu, 15 Jan 2015 18:05:24 GMT</pubDate></item><item><title><![CDATA[Reply to ostream&amp;lt;&amp;lt; in PIMPL on Thu, 15 Jan 2015 19:00:16 GMT]]></title><description><![CDATA[<p>Hallo Stefan,</p>
<p>wenn Du Impl public machst, gibst Du keine Informationen raus, da Du Impl weiterhin einfach in .cpp definieren kannst. Ich finde es eigentlich ungewöhnlich operator&lt;&lt; immer als friend zu deklarieren. Einfach ein forwarding an eine öffentliche Funktion ergibt die gewünschte Syntax:</p>
<pre><code>class A
{
public:
   struct Impl;
private:
   std::unique_ptr&lt;Impl&gt; pImpl;
};

std::ostream&amp; operator&lt;&lt;( std::ostream&amp;, const A&amp; );
</code></pre>
<p>.cpp</p>
<pre><code>class A::Impl 
{
public:
   void print( std::ostream&amp; ) const;
};

std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, const A&amp; a )
{
   a.print( out );
   return out;
}
</code></pre>
<p>Wenn operator&lt;&lt;() aber unbedingt ein template sein muss, dann kommst Du wohl nicht darum herum, Impl im header zu definieren. Es sei den, die Liste der Instanziierungen ist übersichtlich, bekannt und klein; dann könntest Du das template auch in .cpp definieren und explizit instanzieren.</p>
<p>Kleiner Hinweis noch: Bezeichner, die mit einem _ anfangen, gefolgt von einem Großbuchstaben, sind für die Implementierung reserviert. Du dürftest Dich also nicht beschweren, wenn irgend eine Standardlibrary _OStream als Makro definiert.</p>
<p>mfg Torsten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2437768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2437768</guid><dc:creator><![CDATA[Torsten Robitzki]]></dc:creator><pubDate>Thu, 15 Jan 2015 19:00:16 GMT</pubDate></item></channel></rss>