|
Boost : |
From: Neal D. Becker (ndbecker2_at_[hidden])
Date: 2004-03-19 09:13:57
Vladimir Prus wrote:
> Neal D. Becker wrote:
>> Why not a default validator?
>> namespace po = boost::program_options;
>>
>> namespace boost { namespace program_options {
>> template<typename T>
>> void validator<T>::operator()(any& v, const vector<string>& xs)
>> {
>> }
>> }}
>>
>> This solves my immediate problem, which is that (strangely)
>> program_options has a validator for float but not for double.
>
> I think it is addressed. You would just use
>
> value<double>()
>
> and it should work. The previous version had explicit instantiation for
> some types, but it no longer the case.
>
Doesn't work for me:
Here is test program:
#include <boost/program_options.hpp>
#include <iostream>
using namespace std;
namespace po = boost::program_options;
// namespace boost { namespace program_options {
// template<typename T>
// void validator<T>::operator()(any& v, const vector<string>& xs)
// {
// validators::check_first_occurence(v);
// string s(validators::get_single_string(xs));
// try {
// v = any(lexical_cast<T>(s));
// }
// catch(const bad_lexical_cast&) {
// throw validation_error("'" + s + "' doesn't look like right");
// }
// }
// }}
int main (int argc, char** argv) {
try {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level")
("stupidity", po::value<double>(), "set stupidity level")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 1;
}
if (vm.count("compression")) {
cout << "Compression level was set to "
<< vm["compression"].as<int>() << ".\n";
} else {
cout << "Compression level was not set.\n";
}
if (vm.count("stupidity")) {
cout << "Stupidity level was set to "
<< vm["stupidity"].as<double>() << ".\n";
} else {
cout << "Stupidity level was not set.\n";
}
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';
}
}
Result:
make
g++ -g -I /usr/local/src/boost_1_31_0 -o Test1 Test1.cc
-L /usr/local/src/boost_1_31_0/stage/lib -l boost_program_options-gcc
/tmp/cceRudK0.o(.gnu.linkonce.t._ZNK5boost15program_options11typed_valueIdE5parseERNS_3anyERKSt6vectorISsSaISsEE+0x14):
In function
`boost::program_options::typed_value<double>::parse(boost::any&,
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > > const&) const':
/usr/local/src/boost_1_31_0/boost/detail/shared_count.hpp:145: undefined
reference to `boost::program_options::validator<double>::operator(
(boost::any&, std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > > const&)'
collect2: ld returned 1 exit status
make: *** [Test1] Error 1
Uncomment the validator and it works.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk