<?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[optimale typenübergabe]]></title><description><![CDATA[<p>hallo</p>
<p>da es bei kleinen eingebauten typen wie int usw schneller ist eine kopie zu erstellen anstatt eine kontante referenz zu nutzen, habe ich mir etwas kleines dazu zusammengebastelt:</p>
<pre><code>#include &lt;limits&gt;
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;
template&lt;class T&gt;
class my_container
{
	template&lt;class U, bool basic = std::numeric_limits&lt;U&gt;::is_specialized&gt;
	struct helper;
public:
	typedef typename helper&lt;T&gt;::type const_reference;
};
template&lt;class T&gt;
template&lt;class U&gt; 
struct my_container&lt;T&gt;::helper&lt;U, true&gt;
{
	typedef U type;
};
template&lt;class T&gt;
template&lt;class U&gt; 
struct my_container&lt;T&gt;::helper&lt;U, false&gt;
{
	typedef U const&amp; type;
};
template&lt;class T, class U&gt;
struct equal
{
	static const bool value = false;
};
template&lt;class T&gt;
struct equal&lt;T, T&gt;
{
	static const bool value = true;
};
class special_type
{
	special_type(special_type const&amp;);
	special_type&amp; operator= (special_type const&amp;);
public:
	special_type()
	{
	}
	~special_type()
	{
	}
};
int main()
{
	std::cout &lt;&lt; std::boolalpha;
	std::cout &lt;&lt; &quot;int as copy:\t\t&quot; &lt;&lt; equal&lt;int, my_container&lt;int&gt;::const_reference&gt;::value &lt;&lt; '\n';
	std::cout &lt;&lt; &quot;special_type as copy:\t&quot; &lt;&lt; equal&lt;special_type, my_container&lt;special_type&gt;::const_reference&gt;::value &lt;&lt; '\n';
	std::cout &lt;&lt; &quot;float as copy:\t\t&quot; &lt;&lt; equal&lt;float, my_container&lt;float&gt;::const_reference&gt;::value &lt;&lt; '\n';
}
</code></pre>
<p>nun habe ich ein wenig angst da man std::numeric_limts spezialisieren kann und dann für die eigenen - unter umständen nicht kopierbaren typen - ein true bei is_specialized reintun kann. gibt es möglichkeiten dieses problem zu beheben? oder gibt es allgemein bessere methoden um zwischen (T) und (T const&amp;) zu entscheiden bei konstanten referenzen?</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/321030/optimale-typenübergabe</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 13:15:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/321030.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 21 Oct 2013 11:47:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to optimale typenübergabe on Mon, 21 Oct 2013 11:47:25 GMT]]></title><description><![CDATA[<p>hallo</p>
<p>da es bei kleinen eingebauten typen wie int usw schneller ist eine kopie zu erstellen anstatt eine kontante referenz zu nutzen, habe ich mir etwas kleines dazu zusammengebastelt:</p>
<pre><code>#include &lt;limits&gt;
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;
template&lt;class T&gt;
class my_container
{
	template&lt;class U, bool basic = std::numeric_limits&lt;U&gt;::is_specialized&gt;
	struct helper;
public:
	typedef typename helper&lt;T&gt;::type const_reference;
};
template&lt;class T&gt;
template&lt;class U&gt; 
struct my_container&lt;T&gt;::helper&lt;U, true&gt;
{
	typedef U type;
};
template&lt;class T&gt;
template&lt;class U&gt; 
struct my_container&lt;T&gt;::helper&lt;U, false&gt;
{
	typedef U const&amp; type;
};
template&lt;class T, class U&gt;
struct equal
{
	static const bool value = false;
};
template&lt;class T&gt;
struct equal&lt;T, T&gt;
{
	static const bool value = true;
};
class special_type
{
	special_type(special_type const&amp;);
	special_type&amp; operator= (special_type const&amp;);
public:
	special_type()
	{
	}
	~special_type()
	{
	}
};
int main()
{
	std::cout &lt;&lt; std::boolalpha;
	std::cout &lt;&lt; &quot;int as copy:\t\t&quot; &lt;&lt; equal&lt;int, my_container&lt;int&gt;::const_reference&gt;::value &lt;&lt; '\n';
	std::cout &lt;&lt; &quot;special_type as copy:\t&quot; &lt;&lt; equal&lt;special_type, my_container&lt;special_type&gt;::const_reference&gt;::value &lt;&lt; '\n';
	std::cout &lt;&lt; &quot;float as copy:\t\t&quot; &lt;&lt; equal&lt;float, my_container&lt;float&gt;::const_reference&gt;::value &lt;&lt; '\n';
}
</code></pre>
<p>nun habe ich ein wenig angst da man std::numeric_limts spezialisieren kann und dann für die eigenen - unter umständen nicht kopierbaren typen - ein true bei is_specialized reintun kann. gibt es möglichkeiten dieses problem zu beheben? oder gibt es allgemein bessere methoden um zwischen (T) und (T const&amp;) zu entscheiden bei konstanten referenzen?</p>
<p>mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2361832</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2361832</guid><dc:creator><![CDATA[not smart]]></dc:creator><pubDate>Mon, 21 Oct 2013 11:47:25 GMT</pubDate></item><item><title><![CDATA[Reply to optimale typenübergabe on Mon, 21 Oct 2013 12:44:31 GMT]]></title><description><![CDATA[<p>Siehe <a href="http://www.boost.org/doc/libs/1_54_0/libs/utility/call_traits.htm" rel="nofollow">http://www.boost.org/doc/libs/1_54_0/libs/utility/call_traits.htm</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2361848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2361848</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Mon, 21 Oct 2013 12:44:31 GMT</pubDate></item><item><title><![CDATA[Reply to optimale typenübergabe on Mon, 21 Oct 2013 13:19:55 GMT]]></title><description><![CDATA[<blockquote>
<p>da es bei kleinen eingebauten typen wie int usw schneller ist eine kopie zu erstellen anstatt eine kontante referenz zu nutzen</p>
</blockquote>
<p>Es ist besser diese Ausgangsthese zu pruefen und die Groessenordnung von &quot;schneller&quot; bestimmen. Bei boost::call_traits habe ich keine Benchmarktabelle oder eingehende Analyse auf die Schnelle gefunden. Auch verstehe ich nicht, wo du jetzt in deinem Testfall tatsaechlich eine Kopie/Referenz erstellst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2361859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2361859</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 21 Oct 2013 13:19:55 GMT</pubDate></item><item><title><![CDATA[Reply to optimale typenübergabe on Mon, 21 Oct 2013 15:05:57 GMT]]></title><description><![CDATA[<p>danke krümelkacker, soetwas habe ich gesucht.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/21632">@knivil</a>, das habe ich mal vor einiger zeit so aufgeschnappt und seither ist es immer gang und gebe auch in anderen programmcodes, dass z.b. floats per call by copy (auch wenn sie in der funkion nicht geändert werden) übergeben werden und komplexere datenstrukutren mit mehreren membern per call by reference.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2361889</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2361889</guid><dc:creator><![CDATA[not smart]]></dc:creator><pubDate>Mon, 21 Oct 2013 15:05:57 GMT</pubDate></item><item><title><![CDATA[Reply to optimale typenübergabe on Mon, 21 Oct 2013 15:31:51 GMT]]></title><description><![CDATA[<blockquote>
<p>floats per call by copy (auch wenn sie in der funkion nicht geändert werden) übergeben werden und komplexere datenstrukutren mit mehreren membern per call by reference.</p>
</blockquote>
<p>Ja und?<br />
1.) Muss const-ref nicht langsamer sein bei primitiven Typen<br />
2.) Hat es vielleicht nix mit Performance zu tun sondern mit Schreibarbeit<br />
3.) Wie soll denn ein Funktionsaufruf nun konkret aussehen, d.h. wie soll ich dein Teil anwenden? Bis jetzt liefert es nur true/false als Template.<br />
4.) Der Compiler kann von selbst optimieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2361909</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2361909</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 21 Oct 2013 15:31:51 GMT</pubDate></item><item><title><![CDATA[Reply to optimale typenübergabe on Mon, 21 Oct 2013 18:57:56 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>3.) Wie soll denn ein Funktionsaufruf nun konkret aussehen, d.h. wie soll ich dein Teil anwenden? Bis jetzt liefert es nur true/false als Template.</p>
</blockquote>
<p>ist doch offensichtlich, das true und false usw ist nur eine möglichkeit das darzustellen, der typ an und für sich ist das typedef const_reference in my_container&lt;T&gt;.</p>
<p>anwenden würde man das dann so:</p>
<pre><code>template&lt;class T&gt;
class my_container
{
    template&lt;class U, bool basic = std::numeric_limits&lt;U&gt;::is_specialized&gt;
    struct helper;
public:
    typedef typename helper&lt;T&gt;::type const_reference;

    const_reference get() const { /* ... */ }
    T&amp; get() { /* ... */ }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2361959</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2361959</guid><dc:creator><![CDATA[not smart]]></dc:creator><pubDate>Mon, 21 Oct 2013 18:57:56 GMT</pubDate></item><item><title><![CDATA[Reply to optimale typenübergabe on Mon, 21 Oct 2013 20:31:14 GMT]]></title><description><![CDATA[<p>Weils so Spass macht:</p>
<pre><code class="language-cpp">namespace detail {
  template &lt;typename T&gt;
  constexpr 
  typename std::conditional&lt;true,bool,
                            decltype(T::advice_pass_by_value_tag)&gt;::type
  is_tagged(int) { return T::advice_pass_by_value_tag; }
  template &lt;typename T&gt; constexpr bool is_tagged(...) { return false; }
}
template &lt;typename T&gt;
struct advice_pass_by_value
  : std::integral_constant&lt;bool,
                           (std::is_trivially_copyable&lt;T&gt;::value &amp;&amp;
                            sizeof(T) &lt; 2*sizeof(T*))
    || detail::is_tagged&lt;T&gt;(0)&gt; {};

template &lt;typename T&gt;
using copymized = typename std::conditional&lt;advice_pass_by_value&lt;T&gt;::value, T, T const&amp;&gt;::type;

/* Würde man so verwenden:
    copymized&lt;T&gt; get() const { ... }
    T&amp; get() { ... }
/*
// ---

class external_type { char blob[256]; };
template &lt;&gt; struct advice_pass_by_value&lt;external_type&gt; : std::true_type {};

struct integrated_type {
  static constexpr bool advice_pass_by_value_tag = true;
};

struct deepcopy_type {
  int data;
  deepcopy_type(deepcopy_type const&amp;) {}
};

int main()
{
#define VERIFY(boolean, ...)                                                \
  static_assert(advice_pass_by_value&lt;__VA_ARGS__&gt;::value == boolean, #__VA_ARGS__)

  VERIFY(true, int);
  VERIFY(false, std::array&lt;char,256&gt;);
  VERIFY(true, external_type);
  VERIFY(true, integrated_type);
  VERIFY(false, deepcopy_type);
}
</code></pre>
<p>Aber ernsthaft: Jeder Compiler kann das heutzutage selber optimieren. In jedem Fall.</p>
<p>Und get() muss aus Konsistenzgründen const&amp; zurückggeben, damit du eine const-Referenz auf den originalen Wert bilden kannst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2361980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2361980</guid><dc:creator><![CDATA[const_helper]]></dc:creator><pubDate>Mon, 21 Oct 2013 20:31:14 GMT</pubDate></item><item><title><![CDATA[Reply to optimale typenübergabe on Tue, 22 Oct 2013 07:31:48 GMT]]></title><description><![CDATA[<blockquote>
<p>ist doch offensichtlich, das true und false usw ist nur eine möglichkeit das darzustellen, der typ an und für sich ist das typedef const_reference in my_container&lt;T&gt;.</p>
</blockquote>
<p>Wenn es so offensichtlich ist, dann ist es fuer dich doch unproblematisch ein paar kleine Beispiele anzugeben. Ich bezweifle naemlich, das es im Vergleich ueberhaupt nutzbar ist.</p>
<blockquote>
<p>anwenden würde man das dann so</p>
</blockquote>
<p>Ich sehe immer noch keinen Funktionsaufruf. Nimm doch mal ein einfaches Beispiel mit <code>double</code> .</p>
<pre><code class="language-cpp">double my_sqrt( .... x)
{
    return std::sqrt(x ...);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2362030</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2362030</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 22 Oct 2013 07:31:48 GMT</pubDate></item></channel></rss>