<?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[C++: Funktion als &amp;quot;pure&amp;quot; markieren]]></title><description><![CDATA[<p>Ich zitiere mal aus der gcc-Hilfe: &quot;returns a value depending only on its arguments (pure)&quot;</p>
<p>Kann man das auch mit Standard-C++ machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/336859/c-funktion-als-quot-pure-quot-markieren</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 20:36:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/336859.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 21 Feb 2016 10:27:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 10:27:23 GMT]]></title><description><![CDATA[<p>Ich zitiere mal aus der gcc-Hilfe: &quot;returns a value depending only on its arguments (pure)&quot;</p>
<p>Kann man das auch mit Standard-C++ machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488033</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488033</guid><dc:creator><![CDATA[dfsfdf]]></dc:creator><pubDate>Sun, 21 Feb 2016 10:27:23 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 10:43:24 GMT]]></title><description><![CDATA[<p>Ich hab eigentlich keine Ahnung, aber wenn ich das lese</p>
<blockquote>
<p>pure<br />
Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure. For example,</p>
<p>int square (int) __attribute__ ((pure));</p>
<p>says that the hypothetical function square is safe to call fewer times than the program says.</p>
<p>Some of common examples of pure functions are strlen or memcmp. Interesting non-pure functions are functions with infinite loops or those depending on volatile memory or other system resource, that may change between two consecutive calls (such as feof in a multithreading environment).</p>
</blockquote>
<p>Dann zündet bei mir sowas:</p>
<pre><code>#include &lt;iostream&gt;

template&lt;typename T&gt;
constexpr auto sqr(T num) { return num * num; }

int main()
{
    std::cout &lt;&lt; sqr(5) &lt;&lt; '\n';
}
</code></pre>
<p>und sonst kenn ich noch <code>inline</code> , aber ich glaube das ist etwas anderes, da wird halt irgendwie der Funktionsaufruf eliminiert, oder so.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488038</guid><dc:creator><![CDATA[HarteWare]]></dc:creator><pubDate>Sun, 21 Feb 2016 10:43:24 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 10:47:50 GMT]]></title><description><![CDATA[<p>Ich glaube, constexpr ist etwas anderes. Das besagt, dass man den Funktionswert zur Compile-Zeit auswerten kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488039</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488039</guid><dc:creator><![CDATA[sdffds]]></dc:creator><pubDate>Sun, 21 Feb 2016 10:47:50 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 11:15:23 GMT]]></title><description><![CDATA[<p>Soetwas wie gcc hat gibt es nicht im standard. Weiß auch nicht, wozu man das gebrauchen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488040</guid><dc:creator><![CDATA[Techel]]></dc:creator><pubDate>Sun, 21 Feb 2016 11:15:23 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 11:16:34 GMT]]></title><description><![CDATA[<p>sdffds schrieb:</p>
<blockquote>
<p>Ich glaube, constexpr ist etwas anderes. Das besagt, dass man den Funktionswert zur Compile-Zeit auswerten kann.</p>
</blockquote>
<p>Ja, aber da eine constexpr Funktion, die aucht tatsächlich zur Compile-Zeit ausgewertet wird, weder globale Variablen verändern noch nicht-konstante globale Variablen lesen kann, ist sie pure.</p>
<p>Die sqr() Funktion ist pure instantiert für Typen wie int, wo der operator*() auch constexpr ist. Für einen Typen, der beim multiplizieren Debug-Ausgaben macht, ist sie nicht pure, kann dann aber auch nicht zur Compilezeit ausgewertet werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488041</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sun, 21 Feb 2016 11:16:34 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 11:20:29 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>sdffds schrieb:</p>
<blockquote>
<p>Ich glaube, constexpr ist etwas anderes. Das besagt, dass man den Funktionswert zur Compile-Zeit auswerten kann.</p>
</blockquote>
<p>Ja, aber da eine constexpr Funktion, die aucht tatsächlich zur Compile-Zeit ausgewertet wird, weder globale Variablen verändern noch nicht-konstante globale Variablen lesen kann, ist sie pure.</p>
</blockquote>
<p>Aber umgekehrt -- und darauf kommt es mir an -- ist nicht jede pure-Funktion constexpr.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488043</guid><dc:creator><![CDATA[sdffds]]></dc:creator><pubDate>Sun, 21 Feb 2016 11:20:29 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 13:57:07 GMT]]></title><description><![CDATA[<p>dfsfdf schrieb:</p>
<blockquote>
<p>Ich zitiere mal aus der gcc-Hilfe: &quot;returns a value depending only on its arguments (pure)&quot;</p>
<p>Kann man das auch mit Standard-C++ machen?</p>
</blockquote>
<p>Was, dem Compiler einen Tipp geben? Attribute sind natürlich portabel (insofern als dass keine praktische Implementierung das Attribut missverstehen wird). Oder möchtest du eine Funktion zu diesem Verhalten zwingen? Letzteres scheint nicht sinnvoll zu sein, und wäre auch nicht trivial zu implementieren. Vor allem würde ein derartiger Check false positives liefern, da der Rückgabewert durchaus nur von Argumenten abhängen kann, während im Funktionsrumpf ggf. globale Variablen odr-used werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488054</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 21 Feb 2016 13:57:07 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 16:50:18 GMT]]></title><description><![CDATA[<p>Wenn ich so etwas hier lese:</p>
<p>HarteWare schrieb:</p>
<blockquote>
<blockquote>
<p>... Such a function can be subject to common subexpression elimination... says that the hypothetical function square is safe to call fewer times than the program says...</p>
</blockquote>
</blockquote>
<p>... dann interpretiere ich das so, dass der Compiler z.B. so etwas hier machen darf:</p>
<p><code>5 * sqr(x) - sqr(x)</code> =&gt; <code>4 * sqr(x)</code></p>
<p>Das kann durchaus nützlich sein, wenn man irgendwelche arithmetischen Klassen ( <code>std::complex</code> , Matrix/Vektorklassen) bzw. zugehörige Funktionen<br />
implementiert und möglichst optimalen Code herausholen will. Auch wenn es wohl am ehesten was für diejenigen ist, welche die Standardbibliothek für den Compiler implemenhtieren.</p>
<p>Finnegan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488089</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488089</guid><dc:creator><![CDATA[Finnegan]]></dc:creator><pubDate>Sun, 21 Feb 2016 16:50:18 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 19:11:37 GMT]]></title><description><![CDATA[<p>So verstehe ich pure auch. Allerdings hoffe ich mal, dass der Compiler das in den meisten Fällen automatisch rauskriegt, ohne das man ein Attribut dran packt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488125</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sun, 21 Feb 2016 19:11:37 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 20:28:35 GMT]]></title><description><![CDATA[<p>sebi707 schrieb:</p>
<blockquote>
<p>So verstehe ich pure auch. Allerdings hoffe ich mal, dass der Compiler das in den meisten Fällen automatisch rauskriegt, ohne das man ein Attribut dran packt.</p>
</blockquote>
<p>Aber nur, wenn ihm der Code konkret vorliegt. Das Attribut wird wohl dafür gedacht sein, wenn man mehrere Übersetzungseinheiten hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488136</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488136</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Sun, 21 Feb 2016 20:28:35 GMT</pubDate></item><item><title><![CDATA[Reply to C++: Funktion als &amp;quot;pure&amp;quot; markieren on Sun, 21 Feb 2016 20:45:07 GMT]]></title><description><![CDATA[<p>sebi707 schrieb:</p>
<blockquote>
<p>So verstehe ich pure auch. Allerdings hoffe ich mal, dass der Compiler das in den meisten Fällen automatisch rauskriegt, ohne das man ein Attribut dran packt.</p>
</blockquote>
<p>Bei so etwas wie <code>sqr</code> für skalare Typen würde ich auch davon ausgehen.<br />
Wenn <code>T</code> z.B. eine Matrix ist, könnte sich der Compiler schon etwas schwerer tun, zweifelsfrei nachzuweisen, dass man das machen kann.<br />
Ich überlasse solche Sachen auch gerne dem Compiler - wenn eine Implementierung alleridings davon lebt, das der Compiler etwas bestimmtes macht,<br />
dann lasse ich lieber nichts anbrennen, und verwende auch compilerspezifische Attribute. So z.B. schonmal ausgiebige <code>__forceinline</code> bzw.<br />
<code>__attribute__((always_inline))</code> an den passenden Stellen in Code, der mithilfe von Expression Templates SIMD-Instruktionen erzeugt hat<br />
(wäre witzlos wenn sich die Heuristik da gegen Inlining entscheiden würde).</p>
<p>Finnegan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488139</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488139</guid><dc:creator><![CDATA[Finnegan]]></dc:creator><pubDate>Sun, 21 Feb 2016 20:45:07 GMT</pubDate></item></channel></rss>