A recurring const-connundrum
<p>I often find myself having to define two versions of a function in order to have one that is const and one which is non-const (often a getter, but not always).  The two vary only by the fact that the input and output of one is const, while the input and output of the other is non-const.  The guts of the function - the real work, is IDENTICAL.</p>

<p>Yet, for const-correctness, I need them both.  As a simple practical example, take the following:</p>

<pre><code>inline const ITEMIDLIST * GetNextItem(const ITEMIDLIST * pidl)
{
    return pidl ? reinterpret_cast&lt;const ITEMIDLIST *&gt;(reinterpret_cast&lt;const BYTE *&gt;(pidl) + pidl-&gt;mkid.cb) : NULL;
}

inline ITEMIDLIST * GetNextItem(ITEMIDLIST * pidl)
{
    return pidl ? reinterpret_cast&lt;ITEMIDLIST *&gt;(reinterpret_cast&lt;BYTE *&gt;(pidl) + pidl-&gt;mkid.cb) : NULL;
}
</code></pre>

<p>As you can see, they do the same thing.  I can choose to define one in terms of the other using yet more casts, which is more appropriate if the guts - the actual work, is less trivial:</p>

<pre><code>inline const ITEMIDLIST * GetNextItem(const ITEMIDLIST * pidl)
{
    return pidl ? reinterpret_cast&lt;const ITEMIDLIST *&gt;(reinterpret_cast&lt;const BYTE *&gt;(pidl) + pidl-&gt;mkid.cb) : NULL;
}

inline ITEMIDLIST * GetNextItem(ITEMIDLIST * pidl)
{
    return const_cast&lt;ITEMIDLIST *&gt;(GetNextItem(const_cast&lt;const ITEMIDLIST *&gt;(pidl));
}
</code></pre>

<p>So, I find this terribly tedious and redundant.  But if I wish to write const-correct code, then I either have to supply both of the above, or I have to litter my "consumer-code" with const-casts to get around the problems of having only defined one or the other.</p>

<p>Is there a better pattern for this?  What is the "best" approach to this issue in your opinion: </p>

<ul>
<li>providing two copies of a given function - the const and non-const versions</li>
<li>or just one version, and then requiring consumers of that code to do their casts as they will?</li>
</ul>

<p>Or is there a better approach to the issue entirely?
Is there work being done on the language itself to mitigate or obviate this issue entirely?</p>

<p>And for bonus points:</p>

<ul>
<li>do you find this to be an unfortunate by-product of the C++ const-system</li>
<li>or do you find this to be tantamount to touching the very heights of mount Olympus?</li>
</ul>

<p>EDIT:</p>

<p>If I supply only the first - takes const returns const, then any consumer that needs to modify the returned item, or hand the returned item to another function that will modify it, must cast off the constness.</p>

<p>Similarly, if I supply only the second definition - takes non-const and returns non-const, then a consumer that has a const pidl must cast off the constness in order to use the above function, which honestly, doesn't modify the constness of the item itself.</p>

<p>Maybe more abstraction is desirable:</p>

<pre><code>THING &amp; Foo(THING &amp; it);
const THING &amp; Foo(const THING &amp; it);
</code></pre>

<p>I would love to have a construct:</p>

<pre><code>const_neutral THING &amp; Foo(const_neutral THING &amp; it);
</code></pre>

<p>I certainly could do something like:</p>

<pre><code>THING &amp; Foo(const THING &amp; it);
</code></pre>

<p>But that's always rubbed me the wrong way.  I am saying "I don't modify the contents of your THING, but I'm going to get rid of the constness that you entrusted me with silently for you in your code."</p>

<p>Now, a client, which has:</p>

<pre><code>const THING &amp; it = GetAConstThing();
...
ModifyAThing(Foo(it));
</code></pre>

<p>That's just wrong.  GetAConstThing's contract with the caller is to give it a const reference.  The caller is expected NOT TO MODIFY the thing - only use const-operations on it.  Yes, the caller can be evil and wrong and cast away that constness of it, but that's just Evil(tm).</p>

<p>The crux of the matter, to me, is that Foo is const-neutral.  It doesn't actually modify the thing its given, but its output needs to propagate the constness of its argument.</p>

<p>NOTE: edited a 2nd time for formatting.</p>
