<?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[std::swap + reference]]></title><description><![CDATA[<p>Wie der Titel schon sagt, habe ich ein Problem mit std::swap auf Referenzen angewandt - glaube ich zumindest ^^</p>
<p>Folgendes kleines Beispiel:</p>
<pre><code class="language-cpp">template &lt;typename Tfrom, typename Tto, typename Tcapacity_transport, typename Tmoney&gt;
	class TTransportUnit
	{
	private:
		Tfrom  &amp;start;
		Tto    &amp;destination;
		Tcapacity_transport   capacity;
		Tmoney  trip_cost;
	public:
		TTransportUnit (Tfrom &amp;_start, Tto &amp;_destination, Tcapacity_transport _capacity)
			: start(_start), destination(_destination), capacity(_capacity), trip_cost(transport_costs_per_trip)
			{}

		TTransportUnit&amp; operator = (const TTransportUnit &amp;rhs)
		{	//the compiler (MSVC 08) dont want to generate this operator - so we had to write it
			TTransportUnit tmp(rhs);
			swap(tmp);
			return *this;
		}

		void swap (TTransportUnit &amp;rhs) throw()
		{
			std::swap (start, rhs.start);
			std::swap (destination, rhs.destination);
			std::swap (capacity, rhs.capacity);
			std::swap (trip_cost, rhs.trip_cost);
		}
	};

namespace std
{	//specialising std::swap
	template &lt;typename T1, typename T2, typename T3, typename T4&gt;
	void swap (my::TTransportUnit &lt;T1, T2, T3, T4&gt; &amp;lhs, my::TTransportUnit &lt;T1, T2, T3, T4&gt; &amp;rhs) throw()
	{
		lhs.swap (rhs);
	}
}

int main()
{
	typedef typename my::TTransportUnit &lt;THub, TSpoke, Tcapacity, Tmoney&gt; TTransportUnit;
	typedef typename std::vector &lt; std::pair &lt;Tcapacity, TTransportUnit&gt; &gt; TTransportUnitContainer;

         std::vector &lt;TSpoke&gt; spokes;
//füllen
		TTransportUnitContainer transportunits;
		Tcapacity key = /*iwas mit kapazitäten rumrechnen*/;
		transportunits.push_back (std::make_pair (key, transportunit));
//füllen
		std::swap (transportunits[0], transportunits[5]);
}
</code></pre>
<p>und zwar wird manchmal (nicht immer) mein vector of TSpoke verändert (die zugehörigen Objekte werden dort auch getauscht)...<br />
Ich rätsel jetzt schon seit Ewigkeiten, warum es nicht geht und bin iwie zu blöd, das rauszufinden... Es ist aber definitiv im swap-Aufruf verändert.<br />
Falls es wichtig wäre:<br />
Compiler: MSVC 08<br />
OS: Vista 64bit</p>
<p>Wär toll, ne Antwort von euch zu hören ^^</p>
<p>bb : &gt;</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/235745/std-swap-reference</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 03:54:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/235745.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 06 Mar 2009 04:35:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::swap + reference on Fri, 06 Mar 2009 04:35:00 GMT]]></title><description><![CDATA[<p>Wie der Titel schon sagt, habe ich ein Problem mit std::swap auf Referenzen angewandt - glaube ich zumindest ^^</p>
<p>Folgendes kleines Beispiel:</p>
<pre><code class="language-cpp">template &lt;typename Tfrom, typename Tto, typename Tcapacity_transport, typename Tmoney&gt;
	class TTransportUnit
	{
	private:
		Tfrom  &amp;start;
		Tto    &amp;destination;
		Tcapacity_transport   capacity;
		Tmoney  trip_cost;
	public:
		TTransportUnit (Tfrom &amp;_start, Tto &amp;_destination, Tcapacity_transport _capacity)
			: start(_start), destination(_destination), capacity(_capacity), trip_cost(transport_costs_per_trip)
			{}

		TTransportUnit&amp; operator = (const TTransportUnit &amp;rhs)
		{	//the compiler (MSVC 08) dont want to generate this operator - so we had to write it
			TTransportUnit tmp(rhs);
			swap(tmp);
			return *this;
		}

		void swap (TTransportUnit &amp;rhs) throw()
		{
			std::swap (start, rhs.start);
			std::swap (destination, rhs.destination);
			std::swap (capacity, rhs.capacity);
			std::swap (trip_cost, rhs.trip_cost);
		}
	};

namespace std
{	//specialising std::swap
	template &lt;typename T1, typename T2, typename T3, typename T4&gt;
	void swap (my::TTransportUnit &lt;T1, T2, T3, T4&gt; &amp;lhs, my::TTransportUnit &lt;T1, T2, T3, T4&gt; &amp;rhs) throw()
	{
		lhs.swap (rhs);
	}
}

int main()
{
	typedef typename my::TTransportUnit &lt;THub, TSpoke, Tcapacity, Tmoney&gt; TTransportUnit;
	typedef typename std::vector &lt; std::pair &lt;Tcapacity, TTransportUnit&gt; &gt; TTransportUnitContainer;

         std::vector &lt;TSpoke&gt; spokes;
//füllen
		TTransportUnitContainer transportunits;
		Tcapacity key = /*iwas mit kapazitäten rumrechnen*/;
		transportunits.push_back (std::make_pair (key, transportunit));
//füllen
		std::swap (transportunits[0], transportunits[5]);
}
</code></pre>
<p>und zwar wird manchmal (nicht immer) mein vector of TSpoke verändert (die zugehörigen Objekte werden dort auch getauscht)...<br />
Ich rätsel jetzt schon seit Ewigkeiten, warum es nicht geht und bin iwie zu blöd, das rauszufinden... Es ist aber definitiv im swap-Aufruf verändert.<br />
Falls es wichtig wäre:<br />
Compiler: MSVC 08<br />
OS: Vista 64bit</p>
<p>Wär toll, ne Antwort von euch zu hören ^^</p>
<p>bb : &gt;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675006</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 06 Mar 2009 04:35:00 GMT</pubDate></item><item><title><![CDATA[Reply to std::swap + reference on Fri, 06 Mar 2009 06:47:23 GMT]]></title><description><![CDATA[<p>Wär toll, das Problem zu verstehen ^^ <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Im Ernst, worum geht es Dir? Darum, dass die Referenzen (also die Verweisziele) untereinander getauscht werden? Wenn ja, vergiß es, ein Swap zweier Referenzen tauscht immer deren Inhalte. Das Referenzziel kann <strong>nur</strong> bei der Initialisierung festgelegt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675019</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675019</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Fri, 06 Mar 2009 06:47:23 GMT</pubDate></item><item><title><![CDATA[Reply to std::swap + reference on Fri, 06 Mar 2009 07:47:25 GMT]]></title><description><![CDATA[<p>Vermutlich solltest du die Referenzen durch Zeiger ersetzen. Zeiger kannst du dann tauschen wie du magst. Und der Compiler wird sich dann vermutlich auch nichtmehr weigern einen copy-ctor zu erstellen. Tjo...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675037</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675037</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 06 Mar 2009 07:47:25 GMT</pubDate></item><item><title><![CDATA[Reply to std::swap + reference on Fri, 06 Mar 2009 13:44:47 GMT]]></title><description><![CDATA[<p>Der Zuweisungsoperator kann nur automatisch generiert werden, wenn keine konstanten oder nichtkopierbaren Objekte zugewiesen werden müssen. Referenzen sind zwar keine Objekte, aber konstant und müssen somit bei der Definition initialisiert werden.</p>
<p>Also hast du mit dem eigenen Zuweisungsoperator nicht erreicht, was gewollt wäre. Es hat schon einen Grund, warum der Compiler hier reklamiert... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Wie gesagt wären Zeiger hier wohl besser geeignet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675278</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 06 Mar 2009 13:44:47 GMT</pubDate></item><item><title><![CDATA[Reply to std::swap + reference on Fri, 06 Mar 2009 14:29:06 GMT]]></title><description><![CDATA[<p>Ja - nach dem Schlafen ists mir dann auch aufgefallen, dass es eigtl total logisch ist x)<br />
Werde wohl Zeiger nehmen, weils so was wie nen reference-swap ja vermutlich nicht gibt ^^</p>
<p>Danke -.-'</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675318</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 06 Mar 2009 14:29:06 GMT</pubDate></item></channel></rss>