<?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[Gibt es ein &amp;quot;with&amp;quot; in C++]]></title><description><![CDATA[<p>Hallo,</p>
<p>vorher habe ich RealBasic programmiert und da gab es dieses schöne &quot;with&quot;, z.B.<br />
statt</p>
<pre><code>Eigenschaft.FontSize = ...
Eigenschaft.Bold = ...
Eigenschaft.Italic = ...
</code></pre>
<p>gab es das</p>
<pre><code>with(Eigenschaft)(
.FontSize = ...
.Bold = ...
.Italic = ...
)
</code></pre>
<p>Gibt es soetwas auch in C++?</p>
<p>Danke,</p>
<p>Grüße,</p>
<p>Thilo</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/304839/gibt-es-ein-quot-with-quot-in-c</link><generator>RSS for Node</generator><lastBuildDate>Sat, 27 Jun 2026 15:16:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/304839.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 15 Jun 2012 10:59:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 10:59:14 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>vorher habe ich RealBasic programmiert und da gab es dieses schöne &quot;with&quot;, z.B.<br />
statt</p>
<pre><code>Eigenschaft.FontSize = ...
Eigenschaft.Bold = ...
Eigenschaft.Italic = ...
</code></pre>
<p>gab es das</p>
<pre><code>with(Eigenschaft)(
.FontSize = ...
.Bold = ...
.Italic = ...
)
</code></pre>
<p>Gibt es soetwas auch in C++?</p>
<p>Danke,</p>
<p>Grüße,</p>
<p>Thilo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223476</guid><dc:creator><![CDATA[Thilo87]]></dc:creator><pubDate>Fri, 15 Jun 2012 10:59:14 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:02:06 GMT]]></title><description><![CDATA[<p>Nein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223477</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223477</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:02:06 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:03:09 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">{
  auto&amp; o = langer_name_eines_objekts;
  o.FontSize = ...
  o.Bold = ...
  o.Italic = ...
}
</code></pre>
<p>Statt auto (C++2011 Feature) könntest du auch den Typ einsetzen. Den kenne ich in Deinem Fall nur nicht, weswegen ich auto schrieb.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223478</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223478</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:03:09 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:05:39 GMT]]></title><description><![CDATA[<p>Wofür genau meinst du das in C++ zu brauchen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223480</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223480</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:05:39 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:08:23 GMT]]></title><description><![CDATA[<p>Mit C++11 gibt es auch verallgemeinerte Initialisierungslisten:</p>
<pre><code class="language-cpp">struct foo
{
  int a;
  double b;
  const char* c;
};

int main()
{
  foo f = {2, 2.2, &quot;Hallo&quot;};   // Geht schon immer
  f = {1, 1.1, &quot;Welt!&quot;};       // C++11
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2223482</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223482</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:08:23 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:12:20 GMT]]></title><description><![CDATA[<p>Nein, und IMHO sollte es das auch nicht geben, weil es der Idee der Kapselung zuwider läuft.</p>
<p>In C++ gibt es aber Konstruktoren mit Initialisierungsliste:</p>
<pre><code class="language-cpp">struct Eigenschaft {
    Eigenschaft( int fontSize, bool bold, bool italic )
        : FontSize( fontSize )
        , Bold( bold )
        , Italic( italic )
    {}
    int FontSize;
    bool Bold;
    bool Italic;
};
</code></pre>
<p>Vorteil: Du kannst kein Attribut vergessen!</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223483</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223483</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:12:20 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:21:56 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<pre><code class="language-cpp">f = {1, 1.1, &quot;Welt!&quot;};       // C++11
</code></pre>
</blockquote>
<p>Hab ich was verpasst? Ich bin ziemlich sicher, dass das nicht geht. iirc brauchst du hierfür einen entsprechenden Zuweisungsoperator, der einen geeigneten initializer_list-Parameter hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223487</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223487</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:21:56 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:24:58 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<pre><code class="language-cpp">f = {1, 1.1, &quot;Welt!&quot;};       // C++11
</code></pre>
</blockquote>
<p>Hab ich was verpasst? Ich bin ziemlich sicher, dass das nicht geht. iirc brauchst du hierfür einen entsprechenden Zuweisungsoperator, der einen geeigneten initializer_list-Parameter hat.</p>
</blockquote>
<p>Zumindest der GCC frisst das mit -pedantic, dem habe ich das einfach mal geglaubt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223488</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223488</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:24:58 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 11:45:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#define with(x) \
    for(bool once = true; once; once = false) \
        for(auto&amp; _ = x; once;)
</code></pre>
<p>-&gt;</p>
<pre><code class="language-cpp">with(foo)
{
    _.bar = 123;
    _.baz = 456;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2223503</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223503</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Fri, 15 Jun 2012 11:45:36 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 13:04:13 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<pre><code class="language-cpp">f = {1, 1.1, &quot;Welt!&quot;};       // C++11
</code></pre>
</blockquote>
<p>Hab ich was verpasst? Ich bin ziemlich sicher, dass das nicht geht. iirc brauchst du hierfür einen entsprechenden Zuweisungsoperator, der einen geeigneten initializer_list-Parameter hat.</p>
</blockquote>
<p>Wieso? Es gibt doch schon einen Zuweisungsoperator</p>
<pre><code class="language-cpp">foo&amp; foo::operator=(foo const&amp;);
</code></pre>
<p>und den Parameter des Zuweisungsoperators kann ich per &quot;copy list initialization&quot; erzeugen, genauso wie hier:</p>
<pre><code class="language-cpp">foo param = {1, 1.1, &quot;Welt&quot;};
</code></pre>
<p>... dachte ich ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223533</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 15 Jun 2012 13:04:13 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 13:53:53 GMT]]></title><description><![CDATA[<p>Ich bin nicht so ganz sicher, auf die Schnelle finde ich</p>
<blockquote>
<p>5.17 Assignment and compound assignment operators [expr.ass]<br />
...<br />
9 A braced-init-list may appear on the right-hand side of<br />
— an assignment to a scalar, in which case the initializer list shall have at most a single element. The<br />
meaning of x={v}, where T is the scalar type of the expression x, is that of x=T(v) except that no<br />
narrowing conversion (8.5.4) is allowed. The meaning of x={} is x=T().<br />
— an assignment defined by a user-defined assignment operator, in which case the initializer list is passed<br />
as the argument to the operator function.<br />
[ Example:<br />
complex&lt;double&gt; z;<br />
z = { 1,2 }; // meaning z.operator=({1,2})<br />
z += { 1, 2 }; // meaning z.operator+=({1,2})<br />
int a, b;<br />
a = b = { 1 }; // meaning a=b=1;<br />
a = { 1 } = b; // syntax error<br />
—end example ]</p>
</blockquote>
<p>Evtl. sollte man mal schauen, was clang an der Stelle tut, gcc machte ja bei list-Initialisierung bisher noch nicht alles richtig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223566</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223566</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 15 Jun 2012 13:53:53 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 15:00:25 GMT]]></title><description><![CDATA[<p>Ich finde das, was du zitiert hast, unmissverständlich.</p>
<pre><code class="language-cpp">complex&lt;double&gt; c;
c = {2,3};
</code></pre>
<p>ist demnach nichts besonderes, nur äquivalent zu</p>
<pre><code class="language-cpp">c.operator=({2,3});
</code></pre>
<p>was eine <em>copy-list-initialization</em> des operator= Parameters vom Typ complex&lt;double&gt; darstellt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223601</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223601</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 15 Jun 2012 15:00:25 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 15:05:01 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>Ich finde das, was du zitiert hast, unmissverständlich.</p>
<pre><code class="language-cpp">complex&lt;double&gt; c;
c = {2,3};
</code></pre>
<p>ist demnach nichts besonderes, nur äquivalent zu</p>
<pre><code class="language-cpp">c.operator=({2,3});
</code></pre>
<p>was eine <em>copy-list-initialization</em> des operator= Parameters vom Typ complex&lt;double&gt; darstellt.</p>
</blockquote>
<p>Richtig.<br />
Ein Aggregat wie in SeppJs Beispiel hat aber keinen <em>user-defined</em> assignment operator, std::complex dagegen schon (warum eigentlich? doch nicht etwa dieses Problems wegen?)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223603</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223603</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 15 Jun 2012 15:05:01 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 15:02:23 GMT]]></title><description><![CDATA[<p>Jetzt ist die Frage: Kompiliert g++ es einfach nur, oder tut es auch noch das Richtige?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223605</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223605</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 15 Jun 2012 15:02:23 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 15:12:10 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p><em>user-defined</em> assignment operator</p>
</blockquote>
<p>Uppsi! Ich kann mir vorstellen, dass diese Einschränkung keine Absicht war.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223615</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223615</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Fri, 15 Jun 2012 15:12:10 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 16:08:04 GMT]]></title><description><![CDATA[<p>Der entsprechende Text findet sich so formuliert erstmals in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2640.pdf" rel="nofollow">n2640</a> und taucht im Draft n2691 auf.<br />
Obwohl da viel Text steht, wird offenbar nicht explizit auf den Zuweisungsoperator eingegangen. Wahrscheinlich muss man dafür auch noch die früheren Entwürfe anschauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223638</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 15 Jun 2012 16:08:04 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Fri, 15 Jun 2012 19:17:39 GMT]]></title><description><![CDATA[<p>Werner Salomon schrieb:</p>
<blockquote>
<p>Nein, und IMHO sollte es das auch nicht geben, weil es der Idee der Kapselung zuwider läuft.</p>
</blockquote>
<p>Warum läuft es der Idee der Kapselung zuwieder, wenn kurzfristig öffentliche Methoden in einem markierten, lokalen Namensraum, ohne explizite Objektnennung aufgerufen werden können?</p>
<p>Was ist der Unterschied zwischen:</p>
<pre><code class="language-cpp">a.foo();
a.bar();
</code></pre>
<p>und</p>
<pre><code class="language-cpp">with(a){
.foo();
.bar();
}
</code></pre>
<p>Oder hebst du nur auf dem Beispiel ab (in dem Fall wäre deine Aussage auch nicht völlig unproblematisch)? Dann hat aber ca 90% des Threads nichts mit der Frage zu tun <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223711</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223711</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Fri, 15 Jun 2012 19:17:39 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Sat, 16 Jun 2012 10:29:26 GMT]]></title><description><![CDATA[<p>Kellerautomat schrieb:</p>
<blockquote>
<pre><code class="language-cpp">#define with(x) \
    for(bool once = true; once; once = false) \
        for(auto&amp; _ = x; once;)
</code></pre>
<p>-&gt;</p>
<pre><code class="language-cpp">with(foo)
{
    _.bar = 123;
    _.baz = 456;
}
</code></pre>
</blockquote>
<p>Es gab eine bessere Variante (ungetestet)</p>
<pre><code class="language-cpp">#define with(x) for(auto &amp;_=(x);true;break)
</code></pre>
<p>Ein Grundsatz der OOP und der damit verbundenen Kapselung ist das es dem Programmierer einer Klasse möglich ist, auf Falschzustände zu reagieren. Eine struct kann man belegen wie man möchte, genauso muss man nichts initialisieren. Eine Klasse hingegen sollte ständig ihren Zustand überprüfen können um &quot;codenah&quot; einen Fehler zu melden. Deshalb lieber class ohne public member variablen(stattdessen setter und getter mit Parameterüberprüfung), keine structs und somit sind auch keine with-listen nötig außer vielleicht:</p>
<pre><code class="language-cpp">with(object)
{
_.update();
_.copy();
_.draw();
}
</code></pre>
<p>Diese Kurzschreibweisen haben sind in C++ eigentlich nie bewährt. Schau Dir mal Perl-Code an! Kurz und knackig, aber wer den nicht kommentiert kennt sich bald nicht mehr aus. In C++ kommt man dafür mit weniger Kommentaren aus, also Arbeit ist die Gleiche!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223831</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223831</guid><dc:creator><![CDATA[PhilippHToner]]></dc:creator><pubDate>Sat, 16 Jun 2012 10:29:26 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Sat, 16 Jun 2012 10:38:43 GMT]]></title><description><![CDATA[<p>PhilippHToner schrieb:</p>
<blockquote>
<p>Es gab eine bessere Variante (ungetestet)</p>
<pre><code class="language-cpp">#define with(x) for(auto &amp;_=(x);true;break)
</code></pre>
</blockquote>
<p>Das for-Update muss ein Ausdruck sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2223835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223835</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Sat, 16 Jun 2012 10:38:43 GMT</pubDate></item><item><title><![CDATA[Reply to Gibt es ein &amp;quot;with&amp;quot; in C++ on Sat, 16 Jun 2012 11:06:37 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>PhilippHToner schrieb:</p>
<blockquote>
<p>Es gab eine bessere Variante (ungetestet)</p>
<pre><code class="language-cpp">#define with(x) for(auto &amp;_=(x);true;break)
</code></pre>
</blockquote>
<p>Das for-Update muss ein Ausdruck sein.</p>
</blockquote>
<p>Sorry stimmt, es war eine Variant, die gleichzeitig einen Pointer überprüft hat, also es gehen nur Pointer. Ich fand die Variante so eigentlich extrem brauchbar.</p>
<pre><code class="language-cpp">#define with(x) if(auto _=(x))

class A
{
public:
	void foo(){}
};

int main()
{
	A *a = new A;
	with(a)
	{
		_-&gt;foo();
		delete _;
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2223844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2223844</guid><dc:creator><![CDATA[PhilippHToner]]></dc:creator><pubDate>Sat, 16 Jun 2012 11:06:37 GMT</pubDate></item></channel></rss>