Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>const subscript operators of <code>STL</code> containers can return a reference-to-const because they flat out deny calls to it with indexes that do not exist in the container. Behaviour in this case is undefined. Consequently, as a wise design choice, <code>std::map</code> doesn't even provide a const subscript operator overload.</p> <p>QMap tries to be a bit more accommodating, provides a const subscript operator overload as syntactic sugar, runs into the problem with non-existing keys, again tries to be more accomodating, and returns a default-constructed value instead.</p> <p>If you wanted to keep STL's return-by-const-reference convention, you'd need to allocate a static value and return a reference to <em>that</em>. That, however, would be quite at odds with the reentrancy guarantees that <code>QMap</code> provides, so the only option is to return by value. The <code>const</code> there is just sugar coating to prevent some stupid mistakes like <code>constmap["foo"]++</code> from compiling.</p> <p>That said, returning by reference is not always the most efficient way. If you return a fundamental type, or, with more aggressive optimisation, when <code>sizeof(T)&lt;=sizeof(void*)</code>, return-by-value often makes the compiler return the result in a register directly instead of indirectly (address to result in register) or—heaven forbid—on the stack.</p> <p>The other reason (besides premature pessimisation) to prefer pass-by-const-reference, slicing, doesn't apply here, since both <code>std::map</code> and <code>QMap</code> are value-based, and therefore homogeneous. For a heterogeneous container, you'd need to hold pointers, and pointers are fundamental types (except smart ones, of course).</p> <p>That all said, I almost never use the const subscript operator in Qt. Yes, it has nicer syntax than <code>find()</code>+<code>*it</code>, but invariably, you'll end up with <code>count()</code>/<code>contains()</code> calls right in front of the const subscript operator, which means you're doing the binary search <em>twice</em>. And <em>then</em> you won't notice the miniscule differences in return value performance anyway :)</p> <p>For <code>value() const</code>, though, I agree that it should return reference-to-const, defaulting to the reference-to-default-value being passed in as second argument, but I guess the Qt developers felt that was too much magic.</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