boost::program_options : how to declare and validate my own option type when it belongs to a namespace?

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 namespace 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.

Answers


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.


Need Your Help

Problems with VBoxManage syntax

cmd virtualbox

I'm having problems with the syntax in "VBoxManage".

DocuSign Connect Service

docusignapi

I have been using DocuSign Connect feature to get the recipient and envelope status updates.