如何解决“boost :: bad_any_cast:使用boost :: any_cast失败的转换”当使用boost程序选项? [英] How to solve "boost::bad_any_cast: failed conversion using boost::any_cast" when using boost program options?
问题描述
//使用boost程序选项读取命令行和配置文件数据
#include< boost / program_options.hpp>
using namespace std;
using namespace boost;
namespace po = boost :: program_options;
int main(int argc,char * argv [])
{
po :: options_description config(Configuration);
config.add_options()
(IPAddress,i,IP Address)
(Port,p,Port)
;
po :: variables_map vm;
po :: store(po :: parse_command_line(argc,argv,config),vm);
po :: notify(vm);
cout<< Values\\\
;
string address =(vm [IPAddress]。as< std :: string>())。c_str();
string port =(vm [Port]。as< std :: string>())。c_str();
cout<< (vm [IPAddress]。as< string>())。c_str();
cout<< < (vm [Port]。as< string>())。c_str();
return 0;
}
输入的值是否无法打印?
这里是gdb输出,似乎是被抛出的问题:
'boost :: exception_detail :: clone_impl
'
what():boost :: bad_any_cast:failed使用boost :: any_cast转换
编程接收信号SIGABRT,中止。
0x0000003afd835935在/lib64/libc.so.6中的raise()中。
string address =(vm [IPAddress]。as< std :: string>())c_str
是错误发生的地方;我尝试了std :: string和string的结果相同。
testboostpo -i 192.168.1.10 -p 5000
我尝试声明类型,像这样:
config.add_options()
(IPAddress,i,po :: value& std :: string>(),IP Address)
(Port,p,po :: value< std :: string>(),Port);
但错误仍然出现。
解决方案您会看到 boost :: bad_any_cast $ c $因为两个 const char *
参数重载 po :: variables_map
http://www.boost.org/doc/libs/1_53_0/doc/html/boost/program_options/options_descri_idp44766528.html\">po::options_description_easy_init::operator()
不指定 po :: value_semantic
类型,因此将其转换为 std :: string
不管用。如果要将值转换为 std :: string
,并且它是您的应用程序所必需的,请使用 required()
value semantic。
#include< boost / program_options.hpp>
namespace po = boost :: program_options;
int main(int argc,char * argv [])
{
po :: options_description config(Configuration);
config.add_options()
(IPAddress,i,po :: value< std :: string>() - > required(),IP Address)
port,p,po :: value< std :: string>() - > required(),Port)
;
try {
po :: variables_map vm;
po :: store(po :: parse_command_line(argc,argv,config),vm);
po :: notify(vm);
std :: cout<< 值< std :: endl;
const std :: string address = vm [IPAddress]。as< std :: string>();
const std :: string port = vm [Port]。as< std :: string>();
std :: cout<< address:<地址<< std :: endl;
std :: cout<< port:<端口< std :: endl;
} catch(const std :: exception& e){
std :: cerr<< e.what()< std :: endl;
return 1;
}
return 0;
}
注意添加的catch块,因为解析可以)抛出异常。下面是一个示例会话:
samm $ ./a.out
选项'-IPAddress'但缺少
samm $ ./a.out --IPAddress 127.0.0.1
选项'--Port'是必需的,但缺少
samm $ ./a.out --IPAddress 127.0。 0.1 --Port 5000
值
地址:127.0.0.1
port:5000
samm $
这是一个在线演示,显示相同的行为,由COmpile LInk RUn(coliru)提供。
//Using boost program options to read command line and config file data
#include <boost/program_options.hpp>
using namespace std;
using namespace boost;
namespace po = boost::program_options;
int main (int argc, char *argv[])
{
po::options_description config("Configuration");
config.add_options()
("IPAddress,i","IP Address")
("Port,p","Port")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, config),vm);
po::notify(vm);
cout << "Values\n";
string address = (vm["IPAddress"].as<std::string >()).c_str();
string port = (vm["Port"].as<std::string>()).c_str();
cout << (vm["IPAddress"].as< string >()).c_str();
cout << " " << (vm["Port"].as<string>()).c_str();
return 0;
}
Are the inputted values somehow unprintable?
Here is gdb output, seems to be be cast problem:
terminate called after throwing an instance of
'boost::exception_detail::clone_impl
'
what(): boost::bad_any_cast: failed conversion using boost::any_cast
Program received signal SIGABRT, Aborted.
0x0000003afd835935 in raise () from /lib64/libc.so.6
string address = (vm["IPAddress"].as<std::string >()).c_str();
is where the error occurs; I have tried std::string and string with the same results.
testboostpo -i 192.168.1.10 -p 5000
is the command line.
I tried declaring the types, like so:
config.add_options()
("IPAddress,i", po::value<std::string>(), "IP Address")
("Port,p", po::value<std::string>(), "Port");
but the error still occurred.
Could this be a genuine bug?
解决方案 You see the boost::bad_any_cast
exception thrown from the po::variables_map
because the two const char*
argument overload of po::options_description_easy_init::operator()
does not specify a po::value_semantic
type, so converting it to a std::string
will not work. If you want to convert the value to a std::string
, and it is required for your application, use the required()
value semantic.
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main (int argc, char *argv[])
{
po::options_description config("Configuration");
config.add_options()
("IPAddress,i", po::value<std::string>()->required(), "IP Address")
("Port,p", po::value<std::string>()->required(), "Port")
;
try {
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, config),vm);
po::notify(vm);
std::cout << "Values" << std::endl;
const std::string address = vm["IPAddress"].as<std::string>();
const std::string port = vm["Port"].as<std::string>();
std::cout << "address: " << address << std::endl;
std::cout << "port: " << port << std::endl;
} catch ( const std::exception& e ) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
Note the added catch block since parsing can (and will, as you have noticed) throw exceptions. Here is a sample session:
samm$ ./a.out
the option '--IPAddress' is required but missing
samm$ ./a.out --IPAddress 127.0.0.1
the option '--Port' is required but missing
samm$ ./a.out --IPAddress 127.0.0.1 --Port 5000
Values
address: 127.0.0.1
port: 5000
samm$
Here is an online demo showing the same behavior, courtesy of COmpile LInk RUn (coliru).
这篇关于如何解决“boost :: bad_any_cast:使用boost :: any_cast失败的转换”当使用boost程序选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!