<?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[C2446 bei self assignment check]]></title><description><![CDATA[<p>Guten Abend,</p>
<p>ich habe ein Problem beim Self-Assignment-Check, welches mir nicht so richtig klar wird. Ich erhalte einen C2446 Error: Keine Konvertierung von 'const MyClass&lt;int,false&gt; *' in 'MyClass&lt;int,true&gt; *const '. Dabei prüfe ich aber nur die Adressen!?<br />
Die ausgeklammerte Variante macht doch im Prinzip auch nichts anderes, funktioniert aber.</p>
<p>Auch das Überladen des == Operators bringt nichts.</p>
<p>Könnte mir vielleicht einer von euch sagen, was da schief läuft?</p>
<pre><code>template &lt;typename T, bool Condition&gt;
class MyClass
{
    public:

        template &lt;bool TheObjectCondition&gt;
        MyClass &amp;operator = (const MyClass &lt;T, TheObjectCondition&gt; &amp;TheObject)
        {
            if (this == &amp;TheObject)
                return *this;

            return *this;
        }

        /*
        template &lt;bool TheObjectCondition&gt;
        MyClass &amp;operator = (const MyClass &lt;T, TheObjectCondition&gt; &amp;TheObject)
        {
            const void *ThisPtr = this;
            const void *ObjPtr = &amp;TheObject;

            if (ThisPtr == ObjPtr)
                return *this;

            return *this;
        }
        */
};

int main ()
{
    MyClass &lt;int, true&gt; Object1;
    MyClass &lt;int, false&gt; Object2;
    Object1 = Object2;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/332786/c2446-bei-self-assignment-check</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 15:12:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/332786.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 21 May 2015 23:57:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C2446 bei self assignment check on Thu, 21 May 2015 23:57:20 GMT]]></title><description><![CDATA[<p>Guten Abend,</p>
<p>ich habe ein Problem beim Self-Assignment-Check, welches mir nicht so richtig klar wird. Ich erhalte einen C2446 Error: Keine Konvertierung von 'const MyClass&lt;int,false&gt; *' in 'MyClass&lt;int,true&gt; *const '. Dabei prüfe ich aber nur die Adressen!?<br />
Die ausgeklammerte Variante macht doch im Prinzip auch nichts anderes, funktioniert aber.</p>
<p>Auch das Überladen des == Operators bringt nichts.</p>
<p>Könnte mir vielleicht einer von euch sagen, was da schief läuft?</p>
<pre><code>template &lt;typename T, bool Condition&gt;
class MyClass
{
    public:

        template &lt;bool TheObjectCondition&gt;
        MyClass &amp;operator = (const MyClass &lt;T, TheObjectCondition&gt; &amp;TheObject)
        {
            if (this == &amp;TheObject)
                return *this;

            return *this;
        }

        /*
        template &lt;bool TheObjectCondition&gt;
        MyClass &amp;operator = (const MyClass &lt;T, TheObjectCondition&gt; &amp;TheObject)
        {
            const void *ThisPtr = this;
            const void *ObjPtr = &amp;TheObject;

            if (ThisPtr == ObjPtr)
                return *this;

            return *this;
        }
        */
};

int main ()
{
    MyClass &lt;int, true&gt; Object1;
    MyClass &lt;int, false&gt; Object2;
    Object1 = Object2;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2454370</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454370</guid><dc:creator><![CDATA[Boolshit]]></dc:creator><pubDate>Thu, 21 May 2015 23:57:20 GMT</pubDate></item><item><title><![CDATA[Reply to C2446 bei self assignment check on Fri, 22 May 2015 00:13:25 GMT]]></title><description><![CDATA[<p>Das eine ist eben ein Pointer auf MyClass&lt;int, true&gt;, das andere ein Pointer auf MyClass&lt;int, false&gt;. Das sind zwei ganz verschiedene Klassen, bloß ihr Name ist ähnlich. Wenn du</p>
<pre><code>class MeineKlasse1;
class MeineKlasse2;
</code></pre>
<p>hättest, würdest du dich wohl auch nicht wundern, wenn ein Vergleich von Zeigern auf diese Klasse nicht funktioniert, oder? Ebensowenig macht wohl eine Zuweisung von Objekten der einen Klasse zu Objekten der anderen Klasse Sinn: Es sind zwei gänzlich verschiedene Klassen, selbst wenn sie nach der gleichen Vorlage (Template) erzeugt wurden.</p>
<p>Allgemein bezüglich Selbstzuweisung zitiere ich hier mal Exceptional C++:</p>
<p>Herb Sutter schrieb:</p>
<blockquote>
<p>Exception-unsafe&quot; and &quot;poor design&quot; go hand in hand. If a piece of code isn't exception-safe, that's generally okay and can simply be fixed. But if a piece of code cannot be made exception-safe because of its underlying design, that almost always is a signal of its poor design.</p>
<p>Example 1: A function with two different responsibilities is difficult to make exception-safe.</p>
<p>Example 2: <strong>A copy assignment operator that is written in such a way that it must check for self-assignment is probably not strongly exception-safe either</strong></p>
</blockquote>
<p>Hervorhebung durch mich.</p>
<p>Besserer Zuweisungsoperator:</p>
<pre><code>Klasse &amp;operator=(Klasse k)
{
  k.swap(*this);   // Vertausche alle Ressourcen von k und *this
  return *this; 
}
</code></pre>
<p>Kurz und knackig, exceptionsicher, keine Prüfung auf Selbstzuweisung nötig. Nennt sich Copy&amp;Swap Idiom. Der Kopierkonstruktor und Destruktor erledigen die ganze schwere Arbeit bezüglich des Ressourcenhandlings, der Zuweisungsoperator erledigt nur das simple Vertauschen der Ressourcen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454372</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454372</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 22 May 2015 00:13:25 GMT</pubDate></item><item><title><![CDATA[Reply to C2446 bei self assignment check on Fri, 22 May 2015 00:37:28 GMT]]></title><description><![CDATA[<p>Danke dir.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454375</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454375</guid><dc:creator><![CDATA[Boolshit]]></dc:creator><pubDate>Fri, 22 May 2015 00:37:28 GMT</pubDate></item></channel></rss>