英文:
boost::program_options option of type std::optional<string> will not compile
问题
尝试添加类型为 std::optional<string> 的 add_options() 行导致编译错误。boost::program_options 不支持这种类型吗?如何在 boost::program_options 中管理可选选项?
#include <iostream>
#include <optional>
using namespace std;
#include <boost/program_options.hpp>
using namespace boost;
int main(int argc, const char* argv[])
{
cout << "Hello World\n" << boolalpha;
program_options::options_description options_description("options");
options_description.add_options()
("help", "请帮助我使这段代码能够无错误编译。下面的行需要被注释掉以进行干净的编译。boost 不允许 options 的 optional<> 类型吗?感谢您的帮助")
("optional string", program_options::value<std::optional<string>>(), "这个选项是可选的")
;
}
期望干净的编译并且希望 boost::program_options 支持 std::optional<> 类型。
英文:
The line attempting to add_options() of type std::optional<string> results in a compile error. does boost::program_options not support this type? how are optional options managed w/ boost::program_options?
#include <iostream>
#include <optional>
using namespace std;
#include <boost\program_options.hpp>
using namespace boost;
int main(int argc, const char* argv[])
{
cout << "Hello World\n" << boolalpha;
program_options::options_description options_description("options");
options_description.add_options()
("help", "please help me get this code to compile w/o error . the line below needs to be commented out for a clean compile . does boost not permit an options optional<> type ? Thank You Kindly")
("optional string", program_options::value<std::optional<string>>, "this option is optional")
;
}
expected clean compile and boost::program_options to support std::optional<> type
答案1
得分: 1
你忘了调用 value<>()
。
此外,可选选项可以表示为:
- 在
variable_map
中没有存储值 - 或者存储在
variable_map
中的默认值(来自option_description
)
我在类似的情况下解释了背景信息:https://stackoverflow.com/questions/74187581/using-boostoptional-with-multitoken-and-or-composing/74194810#74194810
当然,你可以坚持使用 boost::optional
,例如,因为你想提供 boost::none
作为默认值(虽然这在故事情节中有些牵强附会)。在这种情况下,我建议使用 boost::optional
,因为它受到库支持:
#include <iostream>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(/*int argc, char const* argv[]*/) {
std::cout << std::boolalpha;
po::options_description options_description("options");
options_description.add_options()
("optional string", po::value<boost::optional<std::string>(), "this option is optional")
;
}
如果你必须使用 std::optional<>
支持,你将不得不自行添加它:https://stackoverflow.com/questions/66539770/using-boostprogram-options-with-stdoptional/66548554#66548554
英文:
You forgot to call value<>()
.
Besides, optional options are represented by either
- no value stored inside the
variable_map
- or the default value (from the
option_description
) stored inside thevariable_map
I explained the background in a similar situation here: https://stackoverflow.com/questions/74187581/using-boostoptional-with-multitoken-and-or-composing/74194810#74194810
Of course, you can insist in your case, e.g. because you want to supply boost::none
as the default value (just making things up for the story-line, albeit far-fetched). In that case, I suggest boost::optional
as it enjoys library support:
#include <iostream>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(/*int argc, char const* argv[]*/) {
std::cout << std::boolalpha;
po::options_description options_description("options");
options_description.add_options() //
("optional string", po::value<boost::optional<std::string>>(), "this option is optional") //
;
}
If you must have std::optional<>
support you'll have to add it yourself: https://stackoverflow.com/questions/66539770/using-boostprogram-options-with-stdoptional/66548554#66548554
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论