Search code examples
c++boostboost-program-options

Which C++ language feature is repeating parentheses after function call?


I am using the boost::program_options library and the code below is used to create a description of the options and add options to it:

po::options_description opts("SendFile Options");

    opts.add_options()("help,h", "Print this help message")
        ("other,o","This is the other");

My question is, which C++ language feature allows you to add the separate descriptions directly after calling the add_options function in the form of repeating values contained in parentheses? What is this called, and how would I create a function that works in this manner?


Solution

  • Simplified example:

    #include <string>
    #include <vector>
    #include <iostream>
    
    class Options {
    public:
        Options& operator()(std::string text)
        {
            strings.push_back(text);
            return *this;
    
        }
        std::vector<std::string> strings;
    
    };
    
    int main()
    {
        Options options{};
        options("Some text")
            ("more text")
            ("even more text");
        for(const auto& text : options.strings)
            std::cout << text << '\n';
    }
    

    Produces:

    Some text
    more text
    even more text