views:

36

answers:

1

Using boost::program_options, I can not get my own option type to compile when it is declared inside a namespace. However outside of the workspace it compiles and works fine :

#include <boost/program_options.hpp>
using namespace boost;
using namespace boost::program_options;

struct my_type1 {
    my_type1(int nn) : n(nn) {}
    int n;
};
namespace nm  {
    struct my_type2 {
        my_type2(int nn) : n(nn) {}
        int n;
    };
}

void validate(boost::any& v,
              const std::vector<std::string>& values,
              my_type1*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(my_type1(lexical_cast<int>(s)));
}
void validate(boost::any& v,
              const std::vector<std::string>& values,
              nm::my_type2*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(nm::my_type2(lexical_cast<int>(s)));
}

int main()  {
    options_description desc("options");
    desc.add_options()
        ("m1", value<my_type1>()    , "")
        ("m2", value<nm::my_type2>(), "")
    ;
    return 0;
}

In main(), declaration of option 'm1' compiles but 'm2' does not... What is missing ? I am using boost_1_43_0 with gcc version 4.4.4.

+1  A: 

The validate function should be in the same namespace as your struct my_type, change to:

  namespace nm  {
     void validate(boost::any& v,
                const std::vector<std::string>& values,
                  my_type2*, int)  {
      const std::string& s = validators::get_single_string(values);
      v = any(my_type2(lexical_cast<int>(s)));
    }
  }

the it compiles for me.

Lars
it works for me too - thanks Lars
Francois