Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't believe it's the deficiency of const-correctness per se, but rather the lack of convenient ability to generalize a method over cv-qualifiers (in the same way we can generalize over types via templates). Hypothetically, imagine if you could write something like:</p> <pre><code>template&lt;cvqual CV&gt; inline CV ITEMIDLIST* GetNextItem(CV ITEMIDLIST * pidl) { return pidl ? reinterpret_cast&lt;CV ITEMIDLIST *&gt;(reinterpret_cast&lt;CV BYTE *&gt;(pidl) + pidl-&gt;mkid.cb) : NULL; } ITEMIDLIST o; const ITEMIDLIST co; ITEMIDLIST* po = GetNextItem(&amp;o); // CV is deduced to be nothing ITEMIDLIST* pco = GetNextItem(&amp;co); // CV is deduced to be "const" </code></pre> <p>Now you can actually do this kind of thing with template metaprogramming, but this gets messy real quick:</p> <pre><code>template&lt;class T, class TProto&gt; struct make_same_cv_as { typedef T result; }; template&lt;class T, class TProto&gt; struct make_same_cv_as&lt;T, const TProto&gt; { typedef const T result; }; template&lt;class T, class TProto&gt; struct make_same_cv_as&lt;T, volatile TProto&gt; { typedef volatile T result; }; template&lt;class T, class TProto&gt; struct make_same_cv_as&lt;T, const volatile TProto&gt; { typedef const volatile T result; }; template&lt;class CV_ITEMIDLIST&gt; inline CV_ITEMIDLIST* GetNextItem(CV_ITEMIDLIST* pidl) { return pidl ? reinterpret_cast&lt;CV_ITEMIDLIST*&gt;(reinterpret_cast&lt;typename make_same_cv_as&lt;BYTE, CV_ITEMIDLIST&gt;::result*&gt;(pidl) + pidl-&gt;mkid.cb) : NULL; } </code></pre> <p>The problem with the above is the usual problem with all templates - it'll let you pass object of any random type so long as it has the members with proper names, not just <code>ITEMIDLIST</code>. You can use various "static assert" implementations, of course, but that's also a hack in and of itself.</p> <p>Alternatively, you can use the templated version to reuse the code inside your .cpp file, and then wrap it into a const/non-const pair and expose that in the header. That way, you pretty much only duplicate function signature. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload