<?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[noexcept, wieso nicht auch ohne Funktion?]]></title><description><![CDATA[<p>Hiho,</p>
<p>ich hab' mich gefragt, wieso es eigentlich keine noexcept-Blöcke gibt? Unbenamste und unbedingte Scopes (also einfach nur {}-Paare) sind ja schon jetzt erlaubt, dann würde man diese einfach nur noch als noexcept kennzeichnen. So in etwa würde ich mir das vorstellen:</p>
<pre><code>void foo()
{
	Widget* w = new Widget;

	noexcept
	{
		int a = 5;		
		a = w-&gt;bar(&amp;a);
	}

	delete w;
}
</code></pre>
<p>Ich stelle mir das so vor, dass obiger Code garantiert sicher ist (i.e. keine Memoryleaks erzeugt). Wenn Widget::bar werfen könnte (also nicht noexcept ist), dann würde mir das der Compiler zur Compilezeit mitteilen und ich kann mir das genauer anschauen.<br />
Diese Art von statischer Sicherheit ist es ja, was C++ so großartig macht.</p>
<p>Grüßle</p>
<p>PS: bevor hier die gewöhnliche Armee an Korinthenkackern auftaucht: mir ist sehr wohl bewusst, dass new/delete nicht idiomatisch sind und std::unique_ptr mein konkretes Beispiel löst.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/340389/noexcept-wieso-nicht-auch-ohne-funktion</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 04:09:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/340389.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 04 Nov 2016 19:30:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to noexcept, wieso nicht auch ohne Funktion? on Fri, 04 Nov 2016 19:30:33 GMT]]></title><description><![CDATA[<p>Hiho,</p>
<p>ich hab' mich gefragt, wieso es eigentlich keine noexcept-Blöcke gibt? Unbenamste und unbedingte Scopes (also einfach nur {}-Paare) sind ja schon jetzt erlaubt, dann würde man diese einfach nur noch als noexcept kennzeichnen. So in etwa würde ich mir das vorstellen:</p>
<pre><code>void foo()
{
	Widget* w = new Widget;

	noexcept
	{
		int a = 5;		
		a = w-&gt;bar(&amp;a);
	}

	delete w;
}
</code></pre>
<p>Ich stelle mir das so vor, dass obiger Code garantiert sicher ist (i.e. keine Memoryleaks erzeugt). Wenn Widget::bar werfen könnte (also nicht noexcept ist), dann würde mir das der Compiler zur Compilezeit mitteilen und ich kann mir das genauer anschauen.<br />
Diese Art von statischer Sicherheit ist es ja, was C++ so großartig macht.</p>
<p>Grüßle</p>
<p>PS: bevor hier die gewöhnliche Armee an Korinthenkackern auftaucht: mir ist sehr wohl bewusst, dass new/delete nicht idiomatisch sind und std::unique_ptr mein konkretes Beispiel löst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2514118</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514118</guid><dc:creator><![CDATA[eimalzin]]></dc:creator><pubDate>Fri, 04 Nov 2016 19:30:33 GMT</pubDate></item><item><title><![CDATA[Reply to noexcept, wieso nicht auch ohne Funktion? on Fri, 04 Nov 2016 19:48:57 GMT]]></title><description><![CDATA[<blockquote>
<p>mir ist sehr wohl bewusst, dass new/delete nicht idiomatisch sind und std::unique_ptr mein konkretes Beispiel löst.</p>
</blockquote>
<p>Aber was ist dann überhaupt der Anlass für dieses Feature? Debugging? Reicht IMO nicht für eine Sprachänderung. Schreib doch einfach</p>
<pre><code>struct Call {
    template &lt;typename X&gt;
    Call(X&amp;&amp; x) {std::forward&lt;X&gt;(x)();}
};

#define NOEXCEPT (Call) [&amp;] () noexcept

NOEXCEPT {
    // ...
};
</code></pre>
<p>Oder implementiere ein Attribut, dass <code>noexcept</code> impliziert, und schreib einfach</p>
<pre><code>&lsqb;&lsqb;nothrow&rsqb;&rsqb; {

}
</code></pre>
<p>Momentan ignoriert GCC Statement-Attribute. Müsste man alles anpassen, aber prinzipiell möglich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2514119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514119</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Fri, 04 Nov 2016 19:48:57 GMT</pubDate></item><item><title><![CDATA[Reply to noexcept, wieso nicht auch ohne Funktion? on Sat, 05 Nov 2016 00:53:46 GMT]]></title><description><![CDATA[<p>eimalzin schrieb:</p>
<blockquote>
<p>Wenn Widget::bar werfen könnte (also nicht noexcept ist), dann würde mir das der Compiler zur Compilezeit mitteilen und ich kann mir das genauer anschauen.</p>
</blockquote>
<p>Das ist soweit ich weiss halt leider genau nicht so.<br />
<code>noexcept</code> heisst nur dass das Programm abgebrochen wird falls doch ne Exception fliegt.</p>
<p><a href="http://en.cppreference.com/w/cpp/language/noexcept_spec" rel="nofollow">http://en.cppreference.com/w/cpp/language/noexcept_spec</a></p>
<blockquote>
<p>If a search for a matching exception handler leaves a function marked noexcept or noexcept(true), std::terminate is called immediately.<br />
(...)<br />
Note that a noexcept specification on a function is not a compile-time check; it is merely a method for a programmer to inform the compiler whether or not a function should throw exceptions.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2514126</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514126</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 05 Nov 2016 00:53:46 GMT</pubDate></item><item><title><![CDATA[Reply to noexcept, wieso nicht auch ohne Funktion? on Sat, 05 Nov 2016 02:00:02 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">noexcept
    {
        int a = 5;      
        a = w-&gt;bar(&amp;a);
    }
</code></pre>
<blockquote>
<p>Ich stelle mir das so vor, dass obiger Code garantiert sicher ist (i.e. keine Memoryleaks erzeugt). Wenn Widget::bar werfen könnte (also nicht noexcept ist), dann würde mir das der Compiler zur Compilezeit mitteilen und ich kann mir das genauer anschauen.</p>
</blockquote>
<p>man müsste es wohl eher so verstehen: falls eine exception geworfen wird, terminiere das programm so schnell wie möglich (=verzichte eventuell auf stack-unwinding), was du vielleicht doch nicht willst.</p>
<p>hustbaer schrieb:</p>
<blockquote>
<p>eimalzin schrieb:</p>
<blockquote>
<p>Wenn Widget::bar werfen könnte (also nicht noexcept ist), dann würde mir das der Compiler zur Compilezeit mitteilen und ich kann mir das genauer anschauen.</p>
</blockquote>
<p>Das ist soweit ich weiss halt leider genau nicht so.</p>
</blockquote>
<p>kann, muss aber nicht. und das ist &quot;by-design&quot;, damit (u.a.) so etwas erlaubt ist:</p>
<pre><code class="language-cpp">double sqrt (double a) {
   if (a &lt; 0) throw invalid_argument{};
   return unsafe_sqrt(a);
}

double completely_safe (double a) noexcept {
  return sqrt(abs(a));
}
</code></pre>
<blockquote>
<p>Note that a noexcept specification on a function is not a compile-time check; it is merely a method for a programmer to inform the compiler whether or not a function should throw exceptions.</p>
</blockquote>
<p>es ist schon ein bisschen mehr - es ist nämlich eine ziemlich gute optimier-möglichkeit, aber nicht für compiler (bzw. nur marginal), sondern für libraries wie die standardbibliothek, die nämlich mithilfe std::move_if_noexcept und passend als noexcept deklarierte move-konstruktoren (zumeist implizit) die starke exception-safety garantie weiterhin behalten können (indem sie bei einem nicht-noexcept move-konstruktor auf den copy-ctor zurückgreifen).</p>
<p>noexcept sollte jedenfalls als teil des funktionsinterfaces verstanden werden (auch bei der entscheidung, ob man <code>noexcept</code> hinschreibt oder nicht), und das wird in C++17 auch so formalisiert (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0012r1.html" rel="nofollow">P0012R1: Make exception specifications be part of the type system</a>).</p>
<p>jedenfalls sind das alles gründe, warum <code>noexcept</code> für einen block einfach &quot;unpassend&quot; ist (keine &quot;wirkliche&quot; noexceptions-garantie; fehlendes stack-unwinding; kein implementationsdetail, sondern teil des interfaces; eigentlich hauptsächlich/ursprünglich als last-minute lösung für die starke ausnahmegarantie in std::vector &quot;erfunden&quot; - jedenfalls hat man sich <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2855.html#noexcept-block" rel="nofollow">damals</a> explizit gegen die block-lösung entschieden; und hier ein kommentar von bjarne stroustrup zu der frage <a href="http://www.stroustrup.com/N3202-noexcept.pdf" rel="nofollow">To which extent can noexcept be deduced?</a> und hier ein netter beitrag eines artikels von damals, der die basis für scott meyers effective modern c++/item 14 (declare functions noexcept if they won't emit exceptions) darstellt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2514127</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514127</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Sat, 05 Nov 2016 02:00:02 GMT</pubDate></item><item><title><![CDATA[Reply to noexcept, wieso nicht auch ohne Funktion? on Sat, 05 Nov 2016 19:59:09 GMT]]></title><description><![CDATA[<p>Also eigentlich müsste man Axioms einführen.</p>
<pre><code>template &lt;typename T&gt;
axiom bool NonNegative(T t /*auxiliary variable*/) {
    t &gt; 0; // Expression constraint: must satisfy this for all NonNegative t. For debug purposes.
    UnsignedArithmetic -&gt; T; /* Initialization rule: If the object is initialized by a type that satisfies
                                 the UnsignedArithmetic concept, it satisfies the NonNegative axiom */

}

NonNegative double abs(double);
double sqrt(double x) noexcept requires NonNegative(x);
</code></pre>
<p><code>noexcept requires</code> sagt aus, dass <code>sqrt</code> 's Rumpf nicht werfen wird, falls der Compiler mittels ein paar simpler Regeln zeigen kann, dass das Argument NonNegative ist. Eine Regel haben wir implizit eingeführt: Funktionen und Klassen können NonNegative angegeben werden, und eine explizite Regel besagt, dass vorzeichenlose arithmetische Typen (die wir mittels eines <code>concept</code> s definiert haben) als Initializer ein <code>NonNegative</code> ergeben.</p>
<p>Falls wir nun</p>
<pre><code>double d = sqrt(abs(...));
</code></pre>
<p>schreiben, wird diese Regeln angewandt, und die <a href="http://eel.is/c++draft/except.spec#13" rel="nofollow">Menge der potenziellen Exceptions</a> ist leer.</p>
<p>Wenn diese Regeln ausgeklügelt genug wären, könnten wir tatsächlich <code>noexcept</code> &quot;forcieren&quot;, und etwas korrektere Programme schreiben (natürlich kann der Progger immer noch dämlich sein und ein Axiom für einen Wert angeben, der diesem Axiom nicht gerecht wird).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2514188</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514188</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 05 Nov 2016 19:59:09 GMT</pubDate></item><item><title><![CDATA[Reply to noexcept, wieso nicht auch ohne Funktion? on Sun, 06 Nov 2016 00:11:27 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Also eigentlich müsste man Axioms einführen.</p>
</blockquote>
<p>wenn ich mich recht erinnere, war das doch im allerursprünglichsen proposal zu concepts von annodazumal auch so drin (2006? 2007?) - bloß sind dann ja concepts aus -damals noch C++0x- wieder entfernt worden, dann die &quot;lightweight&quot;-concepts eingeführt worden, die es aber jetzt auch nichtmal nach C++17 schaffen. und statt axioms mit concepts gibt's jetzt dann wohl irgendwas mit preconditions/postconditions in attribut-art, was dem axiom-gedanken aber wieder nicht wirklich nahe kommt. leider hab ich auf die schnelle jetzt nichts besonders aussagekräfitges zum ursprünglichen concept-mit-axiom proposal gefunden (bzw. warum das dann rausgefallen ist). nur so etwas wie</p>
<blockquote>
<p>And that’s it for today. There is much more to be told about axioms. Like, what is wrong with them, what technical issues they cause, how they should be used and how they can be overused. What they cannot do. But I will leave it for the next posts, if this one rises sufficient interest.</p>
</blockquote>
<p><a href="https://akrzemi1.wordpress.com/2012/01/11/concept-axioms-what-for/" rel="nofollow">https://akrzemi1.wordpress.com/2012/01/11/concept-axioms-what-for/</a></p>
<p>wenn ich mich recht erinnere, verschwand das proposal / die axioms aber wegen widerstand der compilerbauer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2514215</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2514215</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Sun, 06 Nov 2016 00:11:27 GMT</pubDate></item></channel></rss>