<?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[ambiguose operatoren]]></title><description><![CDATA[<p>guten abend</p>
<p>folgendes minimalbeispiel:</p>
<pre><code>template&lt;typename Derived&gt;
struct Op
{
	template&lt;typename ScalarType&gt;
	friend Derived operator* (ScalarType const&amp; Lambda, Derived Rhs)
	{
		return Rhs *= Lambda;
	}
};

template&lt;typename T&gt;
struct Foo
{
};

template&lt;typename T&gt;
struct Bar : Op&lt;Bar&lt;T&gt; &gt;
{
	template&lt;typename U&gt;
	Bar operator*= (U)
	{
		return *this;
	}
};

template&lt;typename T&gt;
void operator* (Foo&lt;T&gt;, Bar&lt;T&gt;)
{
}

int main()
{
	Foo&lt;int&gt;() * Bar&lt;int&gt;();
}
</code></pre>
<p>gcc 4.8.1 gibt folgendes aus:</p>
<pre><code>prog.cpp: In function ‘int main()’:
prog.cpp:33:13: error: ambiguous overload for ‘operator*’ (operand types are ‘Foo&lt;int&gt;’ and ‘Bar&lt;int&gt;’)
  Foo&lt;int&gt;() * Bar&lt;int&gt;();
             ^
prog.cpp:33:13: note: candidates are:
prog.cpp:27:6: note: void operator*(Foo&lt;T&gt;, Bar&lt;T&gt;) [with T = int]
 void operator* (Foo&lt;T&gt;, Bar&lt;T&gt;)
      ^
prog.cpp:5:17: note: Derived operator*(const ScalarType&amp;, Derived) [with ScalarType = Foo&lt;int&gt;; Derived = Bar&lt;int&gt;]
  friend Derived operator* (ScalarType const&amp; Lambda, Derived Rhs)
                 ^
</code></pre>
<p><a href="http://ideone.com/8I8KDX" rel="nofollow">http://ideone.com/8I8KDX</a></p>
<p>ich verstehe nicht wie das doppeldeutig sein kann. einer der operatoren nimmt irgendeinen typen an während der andere einen &quot;beschränkteren&quot; typen annimmt. das auflösungssystem in c++ wählt doch immer die am genausten &quot;beschränkte&quot; überladung aus, oder? dann wäre dieser fall hier eindeutig. hat es etwas mit der friend name injection zu tun?</p>
<p>danke im voraus und gruss</p>
<p>ps.: könnt mich gerne aufklären wie man dieses beschränken korrekt bezeichnet. qualifizieren und spezialisieren sind ja beide schon im gebrauch. vll. spezifizieren?</p>
<p>edit: grammatik.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/324977/ambiguose-operatoren</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 07:58:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324977.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 09 Apr 2014 21:19:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ambiguose operatoren on Wed, 09 Apr 2014 21:53:59 GMT]]></title><description><![CDATA[<p>guten abend</p>
<p>folgendes minimalbeispiel:</p>
<pre><code>template&lt;typename Derived&gt;
struct Op
{
	template&lt;typename ScalarType&gt;
	friend Derived operator* (ScalarType const&amp; Lambda, Derived Rhs)
	{
		return Rhs *= Lambda;
	}
};

template&lt;typename T&gt;
struct Foo
{
};

template&lt;typename T&gt;
struct Bar : Op&lt;Bar&lt;T&gt; &gt;
{
	template&lt;typename U&gt;
	Bar operator*= (U)
	{
		return *this;
	}
};

template&lt;typename T&gt;
void operator* (Foo&lt;T&gt;, Bar&lt;T&gt;)
{
}

int main()
{
	Foo&lt;int&gt;() * Bar&lt;int&gt;();
}
</code></pre>
<p>gcc 4.8.1 gibt folgendes aus:</p>
<pre><code>prog.cpp: In function ‘int main()’:
prog.cpp:33:13: error: ambiguous overload for ‘operator*’ (operand types are ‘Foo&lt;int&gt;’ and ‘Bar&lt;int&gt;’)
  Foo&lt;int&gt;() * Bar&lt;int&gt;();
             ^
prog.cpp:33:13: note: candidates are:
prog.cpp:27:6: note: void operator*(Foo&lt;T&gt;, Bar&lt;T&gt;) [with T = int]
 void operator* (Foo&lt;T&gt;, Bar&lt;T&gt;)
      ^
prog.cpp:5:17: note: Derived operator*(const ScalarType&amp;, Derived) [with ScalarType = Foo&lt;int&gt;; Derived = Bar&lt;int&gt;]
  friend Derived operator* (ScalarType const&amp; Lambda, Derived Rhs)
                 ^
</code></pre>
<p><a href="http://ideone.com/8I8KDX" rel="nofollow">http://ideone.com/8I8KDX</a></p>
<p>ich verstehe nicht wie das doppeldeutig sein kann. einer der operatoren nimmt irgendeinen typen an während der andere einen &quot;beschränkteren&quot; typen annimmt. das auflösungssystem in c++ wählt doch immer die am genausten &quot;beschränkte&quot; überladung aus, oder? dann wäre dieser fall hier eindeutig. hat es etwas mit der friend name injection zu tun?</p>
<p>danke im voraus und gruss</p>
<p>ps.: könnt mich gerne aufklären wie man dieses beschränken korrekt bezeichnet. qualifizieren und spezialisieren sind ja beide schon im gebrauch. vll. spezifizieren?</p>
<p>edit: grammatik.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2393728</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2393728</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Wed, 09 Apr 2014 21:53:59 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguose operatoren on Wed, 09 Apr 2014 21:43:04 GMT]]></title><description><![CDATA[<p>Aufklärung:</p>
<p>Du kannst dein Problem auf</p>
<pre><code class="language-cpp">template &lt;typename T&gt; struct A {};
template &lt;typename T&gt; struct B {};
struct C {};

template &lt;typename T&gt; void f(A&lt;T&gt;, B&lt;T&gt;) {std::cout&lt;&lt;&quot;a\n&quot;;}
template &lt;typename T&gt; void f(T, B&lt;C&gt;) {std::cout&lt;&lt;&quot;b\n&quot;;}

int main()
{
  f(A&lt;C&gt;{}, B&lt;C&gt;{});
}
</code></pre>
<p>reduzieren, das ist etwas einfacher zu überlegen.</p>
<p>Also schauen wir, ob a (f(A&lt;T&gt;, B&lt;T&gt;)) oder b (f(T, B&lt;C&gt;)) spezialisierter sind:<br />
A&lt;T&gt; ist spezialisierter als T (A&lt;T&gt; ist immer auch ein T' [für T'=A&lt;T&gt;], aber T ist nicht immer ein A&lt;T'&gt; [z.B. T=int])<br />
A&lt;T&gt; ist allgemeiner als B&lt;C&gt; (B&lt;T&gt; ist nicht immer ein B&lt;C&gt; [z.B. T=int], aber B&lt;C&gt; ist immer ein B&lt;T&gt; [für T=C]).</p>
<p>a ist spezialisierter als b im ersten Argument, aber b spezialisierter als a im zweiten Argument.</p>
<p>Also ist a spezialisierter als b und b spezialisierter als a =&gt; es gibt keine Ordnung zwischen den beiden, beide sind gleich spezialisiert. Damit ist auch die Überladung mehrdeutig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2393737</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2393737</guid><dc:creator><![CDATA[Sexualkundeunterricht]]></dc:creator><pubDate>Wed, 09 Apr 2014 21:43:04 GMT</pubDate></item><item><title><![CDATA[Reply to ambiguose operatoren on Thu, 10 Apr 2014 14:40:25 GMT]]></title><description><![CDATA[<p>das klingt logisch. ich konnte es im code anpassen. danke für die hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2393844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2393844</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Thu, 10 Apr 2014 14:40:25 GMT</pubDate></item></channel></rss>