<?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[RAII automatisieren]]></title><description><![CDATA[<p>Tag zusammen,</p>
<p>gibt es die Möglichkeit RAII halbwegs zu automatisieren?</p>
<p>Klar, ihr werdet jetzt denken: &quot;Junge, RAII ist doch der Automatismus!&quot;<br />
Aber ich meine folgendes (Sinn oder Unsinn des Beispiels sei mal dahingestellt):</p>
<pre><code>void bar(std::istream &amp; is)
{
	throw &quot;What the Excerpt!&quot;;
}

void foo(std::istream &amp; is)
{
	auto oldExceptions = is.exceptions();
	is.exceptions(std::ios_base::badbit | std::ios_base::failbit);

	bar(is);

	is.exceptions(oldExceptions);
}
</code></pre>
<p>Offensichtlich wird der Exceptionsstate des IStreams im Fehlerfall nicht auf den vorherigen Wert zurück gesetzt (klar, will man das überhaupt?!?).</p>
<p>Mit RAII kommt dann folgendes raus:</p>
<pre><code>class ExceptionsStateSaver
{
	std::istream &amp; m_is;
	int m_oldState;

public:
	ExceptionsStateSaver(std::istream &amp; is)
	 : m_is(is), m_oldState(m_is.exceptions())
	{}

	~ExceptionsStateSaver()
	{
		this-&gt;m_is.exceptions(this-&gt;m_oldState);
	}
};

void foo(std::istream &amp; is)
{
	ExceptionsStateSaver e(is);

	is.exceptions(std::ios_base::badbit | std::ios_base::failbit);

	bar(is);
}
</code></pre>
<p>Nicht schwer, vielleicht noch verbesserbar, aber der Gedankengang sollte klar sein.</p>
<p>Aber das Ganze soll auch nur ein Beispiel sein, denn eigentlich will ich nicht für alles und jede Kleinigkeit eine neue RAII Klasse schreiben.</p>
<p>Gibt es da nicht Tricks um so etwas zu vereinfachen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/314311/raii-automatisieren</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 08:55:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/314311.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Feb 2013 08:44:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 08:44:56 GMT]]></title><description><![CDATA[<p>Tag zusammen,</p>
<p>gibt es die Möglichkeit RAII halbwegs zu automatisieren?</p>
<p>Klar, ihr werdet jetzt denken: &quot;Junge, RAII ist doch der Automatismus!&quot;<br />
Aber ich meine folgendes (Sinn oder Unsinn des Beispiels sei mal dahingestellt):</p>
<pre><code>void bar(std::istream &amp; is)
{
	throw &quot;What the Excerpt!&quot;;
}

void foo(std::istream &amp; is)
{
	auto oldExceptions = is.exceptions();
	is.exceptions(std::ios_base::badbit | std::ios_base::failbit);

	bar(is);

	is.exceptions(oldExceptions);
}
</code></pre>
<p>Offensichtlich wird der Exceptionsstate des IStreams im Fehlerfall nicht auf den vorherigen Wert zurück gesetzt (klar, will man das überhaupt?!?).</p>
<p>Mit RAII kommt dann folgendes raus:</p>
<pre><code>class ExceptionsStateSaver
{
	std::istream &amp; m_is;
	int m_oldState;

public:
	ExceptionsStateSaver(std::istream &amp; is)
	 : m_is(is), m_oldState(m_is.exceptions())
	{}

	~ExceptionsStateSaver()
	{
		this-&gt;m_is.exceptions(this-&gt;m_oldState);
	}
};

void foo(std::istream &amp; is)
{
	ExceptionsStateSaver e(is);

	is.exceptions(std::ios_base::badbit | std::ios_base::failbit);

	bar(is);
}
</code></pre>
<p>Nicht schwer, vielleicht noch verbesserbar, aber der Gedankengang sollte klar sein.</p>
<p>Aber das Ganze soll auch nur ein Beispiel sein, denn eigentlich will ich nicht für alles und jede Kleinigkeit eine neue RAII Klasse schreiben.</p>
<p>Gibt es da nicht Tricks um so etwas zu vereinfachen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302440</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302440</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 27 Feb 2013 08:44:56 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 09:30:14 GMT]]></title><description><![CDATA[<p>Sowas wie ScopeGuards (zweite Haelfte des Videos): <a href="http://isocpp.org/blog/2012/12/systematic-error-handling-in-c-andrei-alexandrescu" rel="nofollow">http://isocpp.org/blog/2012/12/systematic-error-handling-in-c-andrei-alexandrescu</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302460</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302460</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 27 Feb 2013 09:30:14 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 09:30:42 GMT]]></title><description><![CDATA[<p>Hab mal sowas ähnliches versucht, allerdings ohne Exceptions. Da kam sowas raus: <a href="http://www.c-plusplus.net/forum/312672-40#2288987" rel="nofollow">Resourceholder</a>. Die meisten fanden aber, dass man das lieber selbst tippen sollte, und dass man da nichts abkürzen kann, gepaart mit wilden Hinweisen auf uniquehandle die ich nicht kenne.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302461</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302461</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Wed, 27 Feb 2013 09:30:42 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 10:11:34 GMT]]></title><description><![CDATA[<p>Mh der ScopeGuard (unter anderem der von Alexandrescu) sieht ja gar nicht mal so schlecht und schwer aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302476</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 27 Feb 2013 10:11:34 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 13:33:13 GMT]]></title><description><![CDATA[<p>Als Hinweis für die, die sich das verlinkte Video von Alexandrescu nicht angesehen haben, sei erwähnt, dass es in der <a href="https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h" rel="nofollow">folly-Bibliothek</a> eine Implementation der C11-Version des ScopeGuard gibt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302561</guid><dc:creator><![CDATA[ananas]]></dc:creator><pubDate>Wed, 27 Feb 2013 13:33:13 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 16:10:53 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p>Aber das Ganze soll auch nur ein Beispiel sein, denn eigentlich will ich nicht für alles und jede Kleinigkeit eine neue RAII Klasse schreiben.</p>
</blockquote>
<p>Eigentlich immer ist das ein Zeichen für schlechtes Design.<br />
Die IOStreams sind einfach nicht gemacht für RAII und Exceptions. Exceptions hat man nachträglich hineingeflickt (wobei das nicht wirklich häufig benutzt wird), aber RAII fehlt halt völlig.</p>
<p>Wenn du einigermassen guten Code schreibst brauchst du sowas nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302618</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302618</guid><dc:creator><![CDATA[iobash]]></dc:creator><pubDate>Wed, 27 Feb 2013 16:10:53 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 16:18:21 GMT]]></title><description><![CDATA[<p>iobash schrieb:</p>
<blockquote>
<p>aber RAII fehlt halt völlig.</p>
</blockquote>
<p>Der Konstruktor konstruiert, der Destruktor zerstört. Was genau fehlt dir für RAII?</p>
<p>Edit: Dass man Exceptions manuell aktivieren muss, stört mich natürlich auch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302622</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302622</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Wed, 27 Feb 2013 16:18:21 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 16:19:32 GMT]]></title><description><![CDATA[<p>Ok, RAII hat man auch teilweise reingeflickt. Siehe die Methoden open() und close().<br />
Dass es nicht vollständig war sieht man an diesem Beispiel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302624</guid><dc:creator><![CDATA[iobash]]></dc:creator><pubDate>Wed, 27 Feb 2013 16:19:32 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 16:26:51 GMT]]></title><description><![CDATA[<p>iobash schrieb:</p>
<blockquote>
<p>Ok, RAII hat man auch teilweise reingeflickt. Siehe die Methoden open() und close().<br />
Dass es nicht vollständig war sieht man an diesem Beispiel.</p>
</blockquote>
<p>Ich glaube, du solltest dich nochmals genauer mit dem Begriff RAII auseinandersetzen. <code>open()</code> und <code>close()</code> sind eben gerade <strong>keine</strong> Beispiele dafür, weil sie manuell aufgerufen werden.</p>
<p>Wie schon von Michael E. gesagt gibt es dafür Konstruktor und Destruktor. RAII wurde also nicht &quot;reingeflickt&quot;, und fehlt schon gar nicht völlig...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302626</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302626</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 27 Feb 2013 16:26:51 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 17:58:11 GMT]]></title><description><![CDATA[<p>iobash schrieb:</p>
<blockquote>
<p>Ok, RAII hat man auch teilweise reingeflickt. Siehe die Methoden open() und close().<br />
Dass es nicht vollständig war sieht man an diesem Beispiel.</p>
</blockquote>
<p>Die Methoden open() und close() gibt es dafür, dass man das Objekt auch später anlegen bzw. früher zerstören <strong>kann</strong>. Es ist aber nicht notwendig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302647</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302647</guid><dc:creator><![CDATA[hjjjjj]]></dc:creator><pubDate>Wed, 27 Feb 2013 17:58:11 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 18:25:05 GMT]]></title><description><![CDATA[<p>Ja, das gleiche ist auch mit Window Klassen und ähnlichem.<br />
RAII heißt lediglich, dass das Objekt vollständig im Destruktor zerstört werden kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302653</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302653</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Wed, 27 Feb 2013 18:25:05 GMT</pubDate></item><item><title><![CDATA[Reply to RAII automatisieren on Wed, 27 Feb 2013 19:59:53 GMT]]></title><description><![CDATA[<p>hjjjjj schrieb:</p>
<blockquote>
<p>Die Methoden open() und close() gibt es dafür, dass man das Objekt auch später anlegen bzw. früher zerstören <strong>kann</strong>. Es ist aber nicht notwendig.</p>
</blockquote>
<p>Nein, so wuerde ich das nicht beschreiben. Datei oeffnen und schliessen ist einfach &quot;unabhaengig&quot; von der Objektkonstruktion.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2302670</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2302670</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 27 Feb 2013 19:59:53 GMT</pubDate></item></channel></rss>