理解他人的编码风格

huangapple go评论71阅读模式
英文:

Understanding other peoples coding style

问题

我正在查看这段代码,并发现了一些特殊之处。
在文件:bin2header.cpp中,您会找到以下代码:

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>());

在"cxxopts.hpp"中定义的"add_options()"函数接受多个参数,用括号"("和")"括起来。有些参数包含2个选项,而其他参数包含3个选项。这是如何工作的?

我看到了对std::initializer_list的使用:
https://en.cppreference.com/w/cpp/utility/initializer_list
但是CPP参考文档上的示例不太一样。

此外,cxxopts::value<string>(),这个value<string>(),在"<"和">"之间是值+数据类型,后面跟着"()"。也在"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);
}

这其中的线索是什么?

英文:

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()
		(&quot;h,help&quot;, &quot;&quot;)
		(&quot;v,version&quot;, &quot;&quot;)
		(&quot;o,output&quot;, &quot;&quot;, cxxopts::value&lt;string&gt;())
		(&quot;n,hname&quot;, &quot;&quot;, cxxopts::value&lt;string&gt;())
		(&quot;s,chunksize&quot;, &quot;&quot;, cxxopts::value&lt;unsigned int&gt;())
		(&quot;d,nbdata&quot;, &quot;&quot;, cxxopts::value&lt;unsigned int&gt;())
		(&quot;c,datacontent&quot;, &quot;&quot;)
		(&quot;f,offset&quot;, &quot;&quot;, cxxopts::value&lt;unsigned long&gt;())
		(&quot;l,length&quot;, &quot;&quot;, cxxopts::value&lt;unsigned long&gt;())
		(&quot;p,pack&quot;, &quot;&quot;, cxxopts::value&lt;unsigned int&gt;())
		(&quot;e,swap&quot;, &quot;&quot;)
		(&quot;stdvector&quot;, &quot;&quot;)
		(&quot;eol&quot;, &quot;&quot;, cxxopts::value&lt;string&gt;());

the function &quot;add_options()&quot; 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&lt;string&gt;(), this value&lt;string&gt;(), value + datatype between "<" and ">" followed by "()".
Also defined in "cxxopts.hpp":

  template &lt;typename T&gt;
  std::shared_ptr&lt;Value&gt;
  value()
  {
    return std::make_shared&lt;values::standard_value&lt;T&gt;&gt;();
  }

  template &lt;typename T&gt;
  std::shared_ptr&lt;Value&gt;
  value(T&amp; t)
  {
    return std::make_shared&lt;values::standard_value&lt;T&gt;&gt;(&amp;t);
  }

What the clue behind that?

答案1

得分: 2

add_options 成员函数返回一个类型为 OptionAdder 的对象。它具有其 operator() 的重载。这意味着它可以像函数一样调用。此调用的结果是对相同 OptionAdder 对象的引用,因此您可以再次像调用函数一样调用它:

// 在 OptionAdder 中:
OptionAdder&amp; operator()(
     const std::string&amp; opts,
     const std::string&amp; desc,
     const std::shared_ptr&lt;const Value&gt;&amp; value
       = ::cxxopts::value&lt;bool&gt;(),
     std::string arg_help = &quot;&quot;
);

这个成员函数可以用两个或三个参数来调用,而不是四个,因为最后两个有默认值。

英文:

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&amp; operator()(
     const std::string&amp; opts,
     const std::string&amp; desc,
     const std::shared_ptr&lt;const Value&gt;&amp; value
       = ::cxxopts::value&lt;bool&gt;(),
     std::string arg_help = &quot;&quot;
);

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

huangapple
  • 本文由 发表于 2023年2月8日 21:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定