Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to specify an empty std::vector as a default parameter?
    text
    copied!<p>I have been reading through <a href="https://stackoverflow.com/questions/703453/optional-function-parameters-use-default-arguments-null-or-overload-the-funct">this similar question</a> and find myself less than satisfied with the responses. I don't have a simple function called "foo"; I have a much more complex function which has very real dangers if parameters are not supplied judiciously. It seems preferable to me for the function to enforce its own rules and ensure system robustness than to lean towards semantic niceties which insist on "one function, one thing to do". I think the favoured responses given to that question reflect programming idealism taken too far, not the reality of certain situations.</p> <p>What I face is:</p> <pre><code>void DoSomethingVeryComplex( const CRect bounds, CPoint startPoint = CPoint(-1,-1) ); </code></pre> <p>Now this happens to satisfy the semantic rules - it is doing some thing, not several things. The one thing it is doing happens to be complex, and multi-staged, but it is still one thing.</p> <p>For that complex thing, it is recommended that the user supply the bounds, and let the function determine its own starting point. Advanced users might want to do the considerable math to determine a specific alternative starting point and supply that... but users beware.</p> <p>And now I have come to see that on rare occasions, it makes sense to supply not one, but several starting points. This changes nothing in the semantic concept of the process involved, because the process is one which spreads from one point to neighbours, to neighbours of neighbours, and so on. So starting from many points is a natural extension.</p> <p>Accordingly, I would prefer to specify:</p> <pre><code>void DoSomethingVeryComplex( const CRect bounds, const std::vector&lt;CPoint&gt; startPoint /* = empty" */ ); </code></pre> <p>so that if the vector is empty, the function determines the start point as before, and if there are any points in the vector, they are used as start point(s).</p> <p>Is there a C++ syntax available which allows me to do this - to specify an empty array as a default parameter?</p>
 

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