Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does boost program_options work?

The weird thing to me is, that boost's options_description uses multi-line code without backslash or semicolon or comma. I did a little research, but found nothing.

(Code taken from official boost's tutorial):

int opt;
po::options_description desc("Allowed options"); 
desc.add_options()
    ("help", "produce help message")
    ("optimization"   , po::value<int>(&opt)->default_value(10), "optimization level")
    ("include-path,I ", po::value< vector<string> >()          , "include path")
    ("input-file     ", po::value< vector<string> >()          , "input file") ;

How is it implemented? Is it a macro?

like image 754
Jack L. Avatar asked Jul 29 '14 11:07

Jack L.


2 Answers

It's a bit of a strange syntax in C++ but if you're familiar with JS (for example), you might be aware of the concept of method chaining. This is a bit like that.

add_options() returns an object with operator() defined. The second line calls operator() on the object returned by the first line. The method returns a reference to the original object, so you can keep calling operator() many times in a row.

Here's a simplified version of how it works:

#include <iostream>

class Example
{
public:
    Example & operator()(std::string arg) {
        std::cout << "added option: " << arg << "\n";
        return *this;
    }
    Example & add_options() {
        return *this;        
    }
};

int main()
{
    Example desc;
    desc.add_options()
        ("first")
        ("second")
        ("third");
    return 0;
}

As pointed out by gbjbaanb in the comments, this is actually quite similar to how chaining of assignments a = b = c = 0 works for classes. It is also similar to the behaviour that is pretty much taken for granted when using ostream::operator<<: you expect to be able to do std::cout << "string 1" << "string 2" << "string 3".

like image 135
Tom Fenech Avatar answered Oct 14 '22 09:10

Tom Fenech


The add_options() method returns object that implements the "()" operator and the () operator in turn returns the same object. See the following code:

class Example
{
public:
    Example operator()(string arg)
    {
        cout << arg << endl;
        return Example();
    }
    Example func(string arg)
    {
        operator()(arg);
    }
};

int main()
{
    Example ex;
    ex.func("Line one")
           ("Line two")
           ("Line three");
    return 0;
}

This is the way it works.

like image 38
user3335 Avatar answered Oct 14 '22 11:10

user3335