英文:
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()
("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?
答案1
得分: 2
add_options
成员函数返回一个类型为 OptionAdder
的对象。它具有其 operator()
的重载。这意味着它可以像函数一样调用。此调用的结果是对相同 OptionAdder
对象的引用,因此您可以再次像调用函数一样调用它:
// 在 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 = ""
);
这个成员函数可以用两个或三个参数来调用,而不是四个,因为最后两个有默认值。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论