0

I'm looking at this code and found some peculiarities.
source: https://sourceforge.net/projects/bin2header/files/v0.4-test/bin2header-0.3.1.tar.xz/download

in the file: bin2header.cpp, you find the following code:

options.add_options()
        ("h,help", "")
        ("v,version", "")
        ("o,output", "", cxxopts::value<string>())
        ("n,hname", "", cxxopts::value<string>())
        ("s,chunksize", "", cxxopts::value<unsigned int>())
        ("d,nbdata", "", cxxopts::value<unsigned int>())
        ("c,datacontent", "")
        ("f,offset", "", cxxopts::value<unsigned long>())
        ("l,length", "", cxxopts::value<unsigned long>())
        ("p,pack", "", cxxopts::value<unsigned int>())
        ("e,swap", "")
        ("stdvector", "")
        ("eol", "", cxxopts::value<string>());

the function "add_options()" defined in "cxxopts.hpp" accepts multiple arguments with "(" and ")".
some contain 2 options and other contain 3.
How does that work?

i saw the usage of this: std::initializer_list
https://en.cppreference.com/w/cpp/utility/initializer_list
but the demonstrated example on CPP reference isdifferent.

Further more, cxxopts::value<string>(), this value<string>(), value + datatype between "<" and ">" followed by "()".
Also defined in "cxxopts.hpp":

  template <typename T>
  std::shared_ptr<Value>
  value()
  {
    return std::make_shared<values::standard_value<T>>();
  }

  template <typename T>
  std::shared_ptr<Value>
  value(T& t)
  {
    return std::make_shared<values::standard_value<T>>(&t);
  }

What the clue behind that?

Harith
  • 4,663
  • 1
  • 5
  • 20
NaturalDemon
  • 934
  • 1
  • 9
  • 21
  • 2
    This is called "overloading" in C++. See your C++ textbook for more information. – Sam Varshavchik Feb 08 '23 at 13:29
  • 4
    Whatever `add_options()` returns overloads `operator()`. Read up on operator overloading. – HolyBlackCat Feb 08 '23 at 13:29
  • 1
    looks like builder pattern with a fancy twist of overloading `()` – 463035818_is_not_an_ai Feb 08 '23 at 13:30
  • 2
    Read about templates and overloading in a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – molbdnilo Feb 08 '23 at 13:31
  • `void foo(int a, int b, int c = 3, int d = 4)` the last two parameters have default arguments. `bar()` calls the template function `bar` with the template parameter `int`. – Eljay Feb 08 '23 at 14:04
  • @Eljay I made a test function with Qt creator called "test" and it's print the templates argument using printf, on the first line after "main()", i may call it via "test('f');", but placing it below my code in main on the last before return 0, i need to call it via "::test('f');", both work, Why would you supply the template parameter up front? it's not a thing you see on a regular base. – NaturalDemon Feb 09 '23 at 13:12
  • The template parameter must be supplied up front if it cannot be deduced. You will see that in source code on a regular basis for all template parameters that cannot be deduced. – Eljay Feb 09 '23 at 13:42
  • @Eljay how you invite some to chat? need some more info on that? – NaturalDemon Feb 09 '23 at 15:41
  • I'm not sure on how the chat facilities work with StackOverflow. I do not know how to instigate a chat, or how to invite people to a chat. I have participated in chat discussions, usually by SO itself recommending to move an *extended comment discussion* into a chat. (SO frowns on extended comment discussions.) – Eljay Feb 09 '23 at 16:43
  • @Eljay indeed, i did however seen it once on the profile of a user, but i was already with him in a chat, his profile said that i could invite him, but not on yours. i have created a room "c++ questions", open to public. – NaturalDemon Feb 09 '23 at 16:55

1 Answers1

2

The add_options member function returns an object of type OptionAdder. This has an overload for its operator(). That means it can be called like a function. The result of this call is a reference to the same OptionAdder object, so you can call it like a function, again:

// in OptionAdder:
OptionAdder& operator()(
     const std::string& opts,
     const std::string& desc,
     const std::shared_ptr<const Value>& value
       = ::cxxopts::value<bool>(),
     std::string arg_help = ""
);

This member function can be called with two or three arguments instead of four because the last two are defaulted.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • thnx, oversaw that one, the overloader. – NaturalDemon Feb 08 '23 at 13:50
  • It took me a moment, but I have tested it with a simple class, as long as you have the "operator()" return the pointer of the object itself "return *this;", you can stack the "function calls", like the example of the question, if i have written this properly or understandable. that's a cool thing. – NaturalDemon Feb 09 '23 at 14:04