Thursday, 15 July 2010

c++ - Benefit from using boost::optional in the following usecase? -


This is a very fundamental question, there is a benefit in using boost :: option in the following scenario:

  int Any action (boost :: optional & lt; const para & gt; value = getDefaultParam ()) {return value- & gt; do something(); }  

or

  int any action (boost: optional & lt; const absolute & gt; value) {if (! Value) Value = getDefaultParam (); Return value- & gt; Dosomething (); }  

Contrary to doing so:

  int Any action (const param and value = getDefaultParam ()) {return value.dosomething (); }  

This is for a specific case, where I know that I'm starting a param object with a default value. Promotion for the ultimate for the API or the API client: can there be any advantage of using alternative?

"post-text" itemprop = "text">

First of all, it is bad:

  int Any action (boost: optional & lt; const Para & gt; value = getDefaultParam ()) {return value-> Dosomething (); }  

some functions can be called from boost :: none , in which case an attempt to use it is an exception Will throw. / P>

Second, if you have boost :: optional , then its value or the API is given to get the default:

  boost :: optional & lt; Const param & amp; & Gt; Value = ...; . Value.value_or (getDefaultParam ()) DoSomething ();  

Edit: On this simple case, can not be worth bringing in optional . If you do something like this:

  int someFunc (const param and amp; value = getDefaultParam ()) {return value.doSomething (); } // somewhere else ... int x = some circumstance? SomeFunc (ABC): someFunc ();  

There really is not a point for alternative Of course, there is not a need for something simple, such as someFunc Can:

  int x = (someCondition? Abc: getDefaultParam ()) doSomething ();  

However, if you have a value available or not, then now the long term storage / tracking required for the Promotion: Alternate may be appropriate or not is required.


No comments:

Post a Comment