6

How do I use a API that takes a list of boost::optional parameters? I have not found any documentation that talks about input parameters,

This and this does not talk about how to handle input parameters. Please let me know what I am missing.

here it is

void myMethod(const boost::optional< std::string >& name,
const boost::optional< std::string >& address,
const boost::optional< boost::string >& description,
const boost::optional< bool >& isCurrent,
const boost::optional< std::string >& ProductName,
const boost::optional< std::string >& Vendor)

Given that, how should I call it? myMethod(,,,,x,y) did not work

roalz
  • 2,699
  • 3
  • 25
  • 42
learningtocode
  • 755
  • 2
  • 13
  • 27

1 Answers1

13

For a function such as:

void myMethod( boost::optional<int>, boost::optional<double> );

You can pass boost::none as any of the boost::optional parameters to specify "none":

myMethod( boost::none, 1.0 );

In C++11 or later, an explicit default-constructed object is even less typing.

myMethod( {}, 1.0 );

If you want to completely omit any mention of the parameter, C++ requires default parameters only after specified parameters.

void myMethod( boost::optional<int> = boost::none_t,
               boost::optional<double> = boost::none_t );

myMethod( 42 ); // (The second parameter will be boost::none_t

You would need to overload your function with different parameter orderings to allow any parameter to be omitted.

void myMethod( boost::optional<int> = boost::none,
               boost::optional<double> = boost::none );

// Alternate parameter ordering
void myMethod( boost::optional<double> d,
               boost::optional<int> i = boost::none )
{
    myMethod( i, d );
}

myMethod( 1.0 ); // (The "int" parameter will be boost::none
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Thank you,Drew! For some reason, I could not find any documentation on how to use the optional parameters, which is why I had to ask the question. If you know of any guides that will help learn using boost libraries, please let me know. – learningtocode Mar 10 '15 at 16:46
  • It complains of primary-expression before ',' token when I tried boost::none_t.Should I cast the none_t to each param type? – learningtocode Mar 10 '15 at 16:58
  • 4
    I had to use `boost::none` in place of missing arguments. [This](http://stackoverflow.com/questions/10934626/what-is-the-rationale-for-boostnone-t-implementation) post is relevant – learningtocode Mar 10 '15 at 23:08
  • I'm slightly confused... (1) why are there no argument variable names in the function declarations? And (2) these approaches fail for me when I try to check in the function if the argument was passed in -- i.e. `if (d) { // do stuff }` fails to convert the boost::optional type to bool, and `if (d.get()) { // do stuff }` fails b/c there is no `get` method. Any ideas @learningtocode et al.? – BoltzmannBrain Jun 06 '18 at 19:56
  • 1
    @BoltzmannBrain please, post that as a question so it (and any answers) get the attention they deserve! – Drew Dormann Jun 07 '18 at 00:04
  • @DrewDormann https://stackoverflow.com/questions/50732272/c-function-with-optional-boostpythonobject-arguments – BoltzmannBrain Jun 07 '18 at 02:48