<?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[Optional argument passed by reference]]></title><description><![CDATA[<p>Hello.</p>
<p>I often need to make optional function arguments passed by reference. I do it this way:</p>
<pre><code class="language-cpp">tData *GetData(tDataSize &amp;size = *reinterpret_cast&lt;tDataSize *&gt;(NULL))
{
  tData *data;
  tDataSize dataSize;

  [...] //Here I calculate data and dataSize

  if(&amp;size != NULL) size=dataSize;
  return data;
};
</code></pre>
<p>Thus, when user needs in data pointer <em>and</em> data size, he calls:</p>
<pre><code class="language-cpp">tDataSize dataSize;
tData *data(GetData(dataSize));
</code></pre>
<p>When he does not needs in size, he just call <code>tData *data(GetData());</code> .</p>
<p>Is it safe doing it this way?<br />
How to make <code>dataSize</code> const in the first case of call? :</p>
<pre><code class="language-cpp">tDataSize dataSize; //How to make this const?
tData const *const data(GetData(dataSize));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/213269/optional-argument-passed-by-reference</link><generator>RSS for Node</generator><lastBuildDate>Tue, 06 Oct 2026 00:43:50 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/213269.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 May 2008 19:54:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 19:54:11 GMT]]></title><description><![CDATA[<p>Hello.</p>
<p>I often need to make optional function arguments passed by reference. I do it this way:</p>
<pre><code class="language-cpp">tData *GetData(tDataSize &amp;size = *reinterpret_cast&lt;tDataSize *&gt;(NULL))
{
  tData *data;
  tDataSize dataSize;

  [...] //Here I calculate data and dataSize

  if(&amp;size != NULL) size=dataSize;
  return data;
};
</code></pre>
<p>Thus, when user needs in data pointer <em>and</em> data size, he calls:</p>
<pre><code class="language-cpp">tDataSize dataSize;
tData *data(GetData(dataSize));
</code></pre>
<p>When he does not needs in size, he just call <code>tData *data(GetData());</code> .</p>
<p>Is it safe doing it this way?<br />
How to make <code>dataSize</code> const in the first case of call? :</p>
<pre><code class="language-cpp">tDataSize dataSize; //How to make this const?
tData const *const data(GetData(dataSize));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1510038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510038</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 15 May 2008 19:54:11 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 20:08:28 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>Is it safe doing it this way?</p>
</blockquote>
<p>No. It calls for undefined behaviour. References <em>always</em> refer to storage, a null pointer does not.</p>
<blockquote>
<p>How to make <code>dataSize</code> const in the first case of call? :</p>
<pre><code class="language-cpp">tDataSize dataSize; //How to make this const?
tData const *const data(GetData(dataSize));
</code></pre>
</blockquote>
<p>please rephrase.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510055</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510055</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 15 May 2008 20:08:28 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 20:37:03 GMT]]></title><description><![CDATA[<p>But I do not use the reference until I check that it is actually <code>!=NULL</code> . Is this still unsafe ??</p>
<p>Before I figured out the NULL-reference, I used this method, but I do not like it:</p>
<pre><code class="language-cpp">tDataSize tempDataSize;

tData *GetData(tDataSize &amp;size = tempDataSize)
{
  tData *data;
  tDataSize dataSize;

  [...] //Here I calculate data and dataSize

  if(&amp;size != &amp;tempDataSize) //Just to illustrate how to make detection of optional argument, so I can skip it calculation for speed-up
    size=dataSize;
  return data;
};
</code></pre>
<p>After that the argument <code>size</code> is optional, but I do not like that I need to store additional temporary instance of <code>tDataSize</code> object.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510076</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 15 May 2008 20:37:03 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 20:39:15 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<blockquote>
<p>How to make <code>dataSize</code> const in the first case of call? :</p>
<pre><code class="language-cpp">tDataSize dataSize; //How to make this const?
tData const *const data(GetData(dataSize));
</code></pre>
</blockquote>
<p>please rephrase.</p>
</blockquote>
<p>I just want <code>dataSize</code> to be protected from further modification after correct size of data stored in it.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510081</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510081</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 15 May 2008 20:39:15 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 20:56:54 GMT]]></title><description><![CDATA[<p>Also I write such things:</p>
<pre><code class="language-cpp">void DoSomething(void)
{
  tData &amp;data(GetData());
  if(&amp;data==NULL) return; //GetData returned NULL
  data.a=1.0;
  data.b=...
}
</code></pre>
<p>I can rewrite it in more &quot;correct&quot; form, but like dots ( <code>.</code> ) and not like arrows ( <code>-&gt;</code> ) :</p>
<pre><code class="language-cpp">void DoSomething(void)
{
  tData *data(GetData());
  if(data==NULL) return; //GetData returned NULL
  data-&gt;a=1.0;
  data-&gt;b=...
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1510091</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510091</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 15 May 2008 20:56:54 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 20:59:09 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>But I do not use the reference until I check that it is actually <code>!=NULL</code> . Is this still unsafe ??</p>
</blockquote>
<p>Dereferencing a null pointer is in itself undefined behaviour, whatever happens after doesn't matter. If an argument is optional, either using a dummy object as shown or a pointer parameter should be preferred. Using an overloaded function would be sufficient as well.</p>
<p>SAn schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<blockquote>
<p>How to make <code>dataSize</code> const in the first case of call? :</p>
<pre><code class="language-cpp">tDataSize dataSize; //How to make this const?
tData const *const data(GetData(dataSize));
</code></pre>
</blockquote>
<p>please rephrase.</p>
</blockquote>
<p>I just want <code>dataSize</code> to be protected from further modification after correct size of data stored in it.</p>
</blockquote>
<p>There is no way to do that. Your function doesn't really (logically) take a parameter, rather it returns two values. The function should return a pair of values - not needing one of the return values is not a problem then. A reference to <a href="http://www.boost.org/doc/libs/1_35_0/libs/tuple/doc/tuple_users_guide.html" rel="nofollow">boost.tuple</a> is in order.</p>
<pre><code class="language-cpp">std::pair&lt;tData*,tDataSize&gt; GetData();

tData* foo = GetData().first; // if we don't need size
tDataSize bar = GetData().second; // if we don't need the pointer
boost::tie(foo,bar) = GetData(); // if we want both
std::pair&lt;tData*,tDataSize&gt; foobar = GetData(); // might be a little bit awkward
// C++0x
auto foobar = GetData(); // would solve that problem
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1510092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510092</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 15 May 2008 20:59:09 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 21:07:17 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>But I do not use the reference until I check that it is actually <code>!=NULL</code> . Is this still unsafe ??</p>
</blockquote>
<p>As camper said before what you are doing is not safe. References always must be bound to valid objects, NULL doesn't point to any one. Use pointers to distinguish between valid and invalid objects, not references.<br />
But this doesn't seem to be your real problem.</p>
<p>SAn schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(&amp;size != &amp;tempDataSize) //Just to illustrate how to make detection of optional argument, so I can skip it calculation for speed-up
</code></pre>
</blockquote>
<p>This is not good practise. Use overloaded functions if you often need optional function arguments passed by reference. Write redundant code in seperate functions. Combine this with strategy pattern or something like that.</p>
<p>Sometimes a const reference may be sufficient for your needs.</p>
<pre><code class="language-cpp">tData* GetData(const tDataSize&amp; size = tDataSize())
</code></pre>
<p>Remember, temporary objects can only be bound to references to const objects.</p>
<p>SAn schrieb:</p>
<blockquote>
<p>I just want <code>dataSize</code> to be protected from further modification after correct size of data stored in it.</p>
</blockquote>
<p>This is not possible at compile time.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510095</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510095</guid><dc:creator><![CDATA[groovemaster]]></dc:creator><pubDate>Thu, 15 May 2008 21:07:17 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 21:13:00 GMT]]></title><description><![CDATA[<p>groovemaster schrieb:</p>
<blockquote>
<p>Lie when your wife is waking. Lie when your belly's aching. Lie when you know she's faking. Lie, sell shoes, and lie.<br />
C-Casts in C++ stinken - basta</p>
</blockquote>
<p>They do make lying that much easier though...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510100</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510100</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 15 May 2008 21:13:00 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 21:21:56 GMT]]></title><description><![CDATA[<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510103</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510103</guid><dc:creator><![CDATA[groovemaster]]></dc:creator><pubDate>Thu, 15 May 2008 21:21:56 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 21:30:12 GMT]]></title><description><![CDATA[<p>How much new information for me!</p>
<p><code>std::pair</code> is standard thing in C++ ??<br />
I found it in <code>&lt;utility&gt;</code> in my system, but I do not know if it will be available on other platforms and if it will be understandable by users of my library. For example, <em>I</em> learned about it a few minutes ago.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510108</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510108</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 15 May 2008 21:30:12 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 22:02:15 GMT]]></title><description><![CDATA[<p>everything in namespace std is standardized and plattform independent (mostly, in the end it depends upon how standard compatible the implementation for the platform is)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1510117</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510117</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Thu, 15 May 2008 22:02:15 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Thu, 15 May 2008 22:11:19 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p><code>std::pair</code> is standard thing in C++ ??</p>
</blockquote>
<p>yes.</p>
<p>Another way to deal with this particular case would be to think of the return value as a view into an array allocated elsewhere. boost has the means for that as well, <a href="http://www.boost.org/doc/libs/1_35_0/libs/multi_array/doc/reference.html" rel="nofollow">Boost.MultiArray</a><br />
for example</p>
<pre><code class="language-cpp">const_multi_array_ref&lt;tData,1&gt; GetData(); // a 1-dimensional array, the ref suffix denotes that it refers to storage allocated elsewhere

const_multi_array_ref&lt;tData,1&gt; data = GetData();
tData const* ptr = data.origin(); // if we absolutely have to have a pointer,
// since multi_array is a proper STL sequence chances are that you won't need it
tDataSize size = data.size();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1510118</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1510118</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 15 May 2008 22:11:19 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Sat, 30 Aug 2008 18:06:33 GMT]]></title><description><![CDATA[<p>groovemaster schrieb:</p>
<blockquote>
<p>SAn schrieb:</p>
<blockquote>
<pre><code class="language-cpp">if(&amp;size != &amp;tempDataSize) //Just to illustrate how to make detection of optional argument, so I can skip it calculation for speed-up
</code></pre>
</blockquote>
<p>This is not good practise. Use overloaded functions if you often need optional function arguments passed by reference. Write redundant code in seperate functions. Combine this with strategy pattern or something like that.</p>
</blockquote>
<p>What is &quot;strategy pattern&quot; and how to use it in this case?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1574057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1574057</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 30 Aug 2008 18:06:33 GMT</pubDate></item><item><title><![CDATA[Reply to Optional argument passed by reference on Mon, 01 Sep 2008 21:56:38 GMT]]></title><description><![CDATA[<p>Hi SAn,</p>
<p>old thread <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>
<p>The strategy pattern is a design pattern. You can find out more about design patterns <a href="http://www.vincehuston.org/dp/" rel="nofollow">here</a>.</p>
<p>The strategy pattern is useful if you have an algorithm or more general code that needs different implementations (strategy) at a certain place depending on the input or your needs. You simply abstract these implementations in an interface. It is like a kind of &quot;macro parameter&quot;.<br />
I don't know if this will be really helpful for your application due to the little code you posted. But it looks a bit like. Just take it as a suggestion for better designs.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1575064</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1575064</guid><dc:creator><![CDATA[groovemaster]]></dc:creator><pubDate>Mon, 01 Sep 2008 21:56:38 GMT</pubDate></item></channel></rss>