<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Probably impossible, but is there any way to do this in C/C++?</title>
		<link>http://www.allegro.cc/forums/view/613585</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 05 Dec 2013 03:28:34 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Verilog 2001 and SystemVerilog have an amazing feature. <b>Pass by name.</b></p><p>Say you have:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> complex_function<span class="k2">(</span><span class="k1">int</span> x, <span class="k1">int</span> y, <span class="k1">int</span> z, <span class="k1">int</span> object_ID, <span class="k1">int</span> state_ID, ...<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

You can do:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
<span class="k1">int</span> x <span class="k3">=</span> <span class="n">3</span><span class="k2">;</span>
<span class="k1">int</span> y <span class="k3">=</span> <span class="n">5</span><span class="k2">;</span>
<span class="k1">int</span> z <span class="k3">=</span> <span class="n">26</span><span class="k2">;</span>
<span class="k1">int</span> t1 <span class="k3">=</span> <span class="n">1001</span><span class="k2">;</span>
<span class="k1">int</span> t2 <span class="k3">=</span> <span class="n">1020</span><span class="k2">;</span>

complex_function<span class="k2">(</span>.x<span class="k2">(</span>x<span class="k2">)</span>, .y<span class="k2">(</span>y<span class="k2">)</span>, .z<span class="k2">(</span>z<span class="k2">)</span>, .t2<span class="k2">(</span>object_ID<span class="k2">)</span>, .t1<span class="k2">(</span>state_ID<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Which accomplishes two things. 1, it allows you to arrange your arguments however is most convenient. And even more important, 2, it forces you and the compiler to know that the argument being passed is exactly what you intend into exactly the variable you intend. </p><p>No worry looking up the definition of blit, or other hard to remember definitions. And more importantly, zero chance of thinking you know it and getting it wrong.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Mon, 02 Dec 2013 06:51:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Pretty much any problem you can think of, the sadists/masochists over at Boost will have came up with a <a href="http://www.boost.org/doc/libs/1_55_0/libs/parameter/doc/html/index.html">&quot;solution&quot;</a> for it. Alternatively, named parameter idiom always &quot;works&quot; too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 02 Dec 2013 06:59:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613585/993471#target">Chris Katko</a> said:</div><div class="quote"><p> No worry looking up the definition of blit, or other hard to remember definitions. And more importantly, zero chance of thinking you know it and getting it wrong</p></div></div><p>You really ought to get a better IDE if you have to actually look that up somewhere :/
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Tue, 03 Dec 2013 03:36:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I didn&#39;t get it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OICW)</author>
		<pubDate>Tue, 03 Dec 2013 04:46:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A typical IDE code editor shows you the parameter list when you have typed <tt>blit(</tt></p><p><span class="remote-thumbnail"><span class="json">{"name":"608133","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/626294f92bd145e41efc83545a8700d5.png","w":432,"h":58,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/2\/626294f92bd145e41efc83545a8700d5"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/6/2/626294f92bd145e41efc83545a8700d5-240.jpg" alt="608133" width="240" height="32" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Wed, 04 Dec 2013 02:14:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613585/993565#target">Johan Halmén</a> said:</div><div class="quote"><p>
A typical IDE code editor shows you the parameter list when you have typed blit(
</p></div></div><p>
You know, that&#39;s true. I haven&#39;t used a good one in a long time and I forgot that side of it. One of the reasons I use Geany is that it&#39;s very fast and responsive, but perhaps I should look into a more exhaustive IDE.</p><p>That point aside, Pass by Name is still a very useful feature. So much so that in, say, Verilog, it&#39;s the proper way to do parameter passing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 04 Dec 2013 06:24:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have to type &quot;man blit&quot; into the console. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Wed, 04 Dec 2013 06:25:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Name parameter allows for optional parameters without having a bunch of overloads that C++ has (and they actually allow for more combinations than overloads allow actually)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MiquelFire)</author>
		<pubDate>Wed, 04 Dec 2013 08:06:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>spam bot gone rogue leaves spammy ad for mobile jammers after joining allegro.cc <a href="https://www.allegro.cc/">https://www.allegro.cc/</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 04 Dec 2013 10:31:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In practice, I&#39;d say that named parameters are only relevant for extremely stable APIs, and in that case you will probably memorize the API and not need something like this. In cases where there are several optional parameters then I guess it could be useful. Often this is accomplished in dynamically-typed languages that support it with an objects&#39; properties. The same thing can be accomplished in statically-typed languages, though it comes at the expense of having to know the parameter types. And if the option/parameter names ever change then it can domino into a lot of maintenance. I would generally avoid abusing this. Most of the time, filling in nothing/null/zero for optional parameters that you don&#39;t need should suffice well enough. If you find that your API is too complex or too much work with this then perhaps you need to rethink your API. You can always offer alternative overloads, or factories, etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Wed, 04 Dec 2013 13:18:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613585/993593#target">bamccaig</a> said:</div><div class="quote"><p>
In practice, I&#39;d say that named parameters are only relevant for extremely stable APIs, and in that case you will probably memorize the API and not need something like this. In cases where there are several optional parameters then I guess it could be useful. Often this is accomplished in dynamically-typed languages that support it with an objects&#39; properties. The same thing can be accomplished in statically-typed languages, though it comes at the expense of having to know the parameter types. And if the option/parameter names ever change then it can domino into a lot of maintenance. I would generally avoid abusing this. Most of the time, filling in nothing/null/zero for optional parameters that you don&#39;t need should suffice well enough. If you find that your API is too complex or too much work with this then perhaps you need to rethink your API. You can always offer alternative overloads, or factories, etc.
</p></div></div><p>
All of your points are true. But imagine an API for creating your object in a game.</p><p>You have x, y, x_vel, y_vel, facing_direction, type_of_object, object_ID, and more.</p><div class="source-code snippet"><div class="inner"><pre>spawn<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, DIR_NORTH, OBJ_TELEPORTER, <span class="n">100012</span>, <span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,...many optional parameters<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Isn&#39;t super confusing, but:</p><div class="source-code snippet"><div class="inner"><pre>spawn<span class="k2">(</span><span class="n">0</span>,<span class="n">0</span><span class="k2">)</span> <span class="k2">:</span>
   velocity<span class="k2">(</span><span class="n">2</span>.<span class="n">5</span>, <span class="n">2</span>.<span class="n">5</span><span class="k2">)</span>,
   direction<span class="k2">(</span>DIR_NORTH<span class="k2">)</span>,
   ID<span class="k2">(</span><span class="n">10015</span><span class="k2">)</span>
</pre></div></div><p>

is pretty sweet for not bothering with optionals, and the exact variables/methods being used are clear. Which is very useful for people who are maintaining other people&#39;s code. It helps clarify what&#39;s going on. There&#39;s also no chance of accidentally removing one too many &quot;,0&quot;  and ending up with a run time error.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 04 Dec 2013 13:34:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Perhaps, but in a language that is known for relative speed this level of dynamic magic comes at a significant cost. It basically forces you to assess why you&#39;re using a static language at all if what you really want is dynamic. Proper dynamic languages do it much simpler and will be much less error-prone. They can also generally be bound closely to the metal with some C glue where appropriate.</p><p>As a rule, I&#39;d say that if your constructor/initializer (or any &quot;subroutine&quot; in the general sense) has an unreasonable number of parameters then it&#39;s probably also a sign that you&#39;re doing it wrong and that your &quot;thing&quot; has too much responsibility. Also keep in mind that a &quot;factory&quot; object is a good solution for centralizing the simplification of a mess for you. It lets you put it in one place, and reap the benefits of a simplified interface, without negatively affecting the design of the &quot;real&quot; thing.</p><p>I encourage you to experiment anyway because that&#39;s the only way that you&#39;ll be able to assess their fitness for a particular purpose. That said, if what you want is something more dynamic then consider a dynamically-typed language. I think it&#39;s pretty well established these days that Real(tm) programmers use the best tool for the job, and that even &quot;scripting&quot; languages are programming languages.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Wed, 04 Dec 2013 13:44:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You could always pass a struct. With C99:
</p><div class="source-code snippet"><div class="inner"><pre>f<span class="k2">(</span><span class="k2">(</span>params_t<span class="k2">)</span><span class="k2">{</span>.y <span class="k3">=</span> <span class="n">1</span>, .x <span class="k3">=</span> <span class="n">1</span><span class="k2">}</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Yeah, not quite.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613585/993595#target">bamccaig</a> said:</div><div class="quote"><p>
It basically forces you to assess why you&#39;re using a static language at all if what you really want is dynamic.
</p></div></div><p>

readable != dynamic</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Proper dynamic languages do it much simpler and will be much less error-prone.
</p></div></div><p>

less error-prone != dynamic
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Wed, 04 Dec 2013 14:36:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613585/993594#target">Chris Katko</a> said:</div><div class="quote"><p>All of your points are true. But imagine an API for creating your object in a game.</p><p>You have x, y, x_vel, y_vel, facing_direction, type_of_object, object_ID, and more.</p><p><span class="source-code">spawn<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, DIR_NORTH, OBJ_TELEPORTER, <span class="n">100012</span>, <span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,...many optional parameters<span class="k2">)</span><span class="k2">;</span></span></p><p>Isn&#39;t super confusing, but:
</p><div class="source-code snippet"><div class="inner"><pre>spawn<span class="k2">(</span><span class="n">0</span>,<span class="n">0</span><span class="k2">)</span> <span class="k2">:</span>
   velocity<span class="k2">(</span><span class="n">2</span>.<span class="n">5</span>, <span class="n">2</span>.<span class="n">5</span><span class="k2">)</span>,
   direction<span class="k2">(</span>DIR_NORTH<span class="k2">)</span>,
   ID<span class="k2">(</span><span class="n">10015</span><span class="k2">)</span>
</pre></div></div></div></div><p>
Problem is that parameter names in function declarations are completely superfluous in C/C++. It&#39;s completely valid to declare the same function multiple times, each with different parameter names. You don&#39;t even need to give a name to a parameter in a declaration (or definition in C++, if you don&#39;t use the parameter).</p><p>As for your example, I think the &quot;proper&quot; C++ way would be something like:<br /><span class="source-code">object.spawn<span class="k2">(</span><span class="n">0</span>,<span class="n">0</span><span class="k2">)</span>.velocity<span class="k2">(</span><span class="n">2</span>.<span class="n">5</span>, <span class="n">2</span>.<span class="n">5</span><span class="k2">)</span>.direction<span class="k2">(</span>DIR_NORTH<span class="k2">)</span>.ID<span class="k2">(</span><span class="n">10015</span><span class="k2">)</span><span class="k2">;</span></span><br />Each member function would return a reference to (or copy of) itself, which allows you to call another setter, etc, and get the resulting object.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Thu, 05 Dec 2013 01:07:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613585/993611#target">Kitty Cat</a> said:</div><div class="quote"><p>
Problem is that parameter names in function declarations are completely superfluous in C/C++. It&#39;s completely valid to declare the same function multiple times, each with different parameter names. You don&#39;t even need to give a name to a parameter in a declaration (or definition in C++, if you don&#39;t use the parameter).
</p></div></div><p>
It&#39;s not that pass-by-name should be always used. It&#39;s that I think it should be an <b>option</b> naturally supported by the compiler/language instead of having to write templates to add functionality missing from the language itself. Once you use an in-language thing to model something that should be internal, you end up with template error messages that are extremely cryptic and long for the simplest error. You also lose the ability for the compiler to handle the &quot;non-standard language feature&quot; in the most optimal-to-date way. A compiler can&#39;t understand your additions to a language, so it can&#39;t optimize it the way it can if it was explicit.</p><p>Even the &quot;worst thing ever&quot; the goto statement is still in C/C++ because it has occasional uses where traditional branching would explode trying to model it. </p><p>So I don&#39;t see any drawback to supporting something that goes all the back to ALGOL in 1960 if it leads to cleaner syntax for situations that are complex without it. Especially functions with many optional parameters.</p><p>That being said, I never started this thread expecting someone to write a letter to the C++ committee.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Thu, 05 Dec 2013 01:22:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/613585/993611#target">Kitty Cat</a> said:</div><div class="quote"><p>
As for your example, I think the &quot;proper&quot; C++ way would be something like:
</p><div class="source-code snippet"><div class="inner"><pre>object.spawn<span class="k2">(</span><span class="n">0</span>,<span class="n">0</span><span class="k2">)</span>.velocity<span class="k2">(</span><span class="n">2</span>.<span class="n">5</span>, <span class="n">2</span>.<span class="n">5</span><span class="k2">)</span>.direction<span class="k2">(</span>DIR_NORTH<span class="k2">)</span>.ID<span class="k2">(</span><span class="n">10015</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Each member function would return a reference to (or copy of) itself, which allows you to call another setter, etc, and get the resulting object. 
</p></div></div><p>
While this is nice, these are just setters and they leave the object incompletely initialized until every setter is used. It&#39;s just a trick. You need to use a constructor, because they allow both for member initialization, and parameters as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 05 Dec 2013 03:28:34 +0000</pubDate>
	</item>
</rss>
