Boost Custom Validator for Enum

I am trying to validate command line input to an Enum that I've defined, but get compiler errors. I have used Handle complex options with Boost's program_options as an example to work from.

namespace po = boost::program_options;

namespace Length

enum UnitType


void validate(boost::any& v, const std::vector<std::string>& values, Length::UnitType*, int)
    Length::UnitType unit;

    if (values.size() < 1)
        throw boost::program_options::validation_error("A unit must be specified");

    // make sure no previous assignment was made
    //po::validators::check_first_occurence(v); // tried this but compiler said it couldn't find it
    std::string input =;
    //const std::string& input = po::validators::get_single_string(values); // tried this but compiler said it couldn't find it

    // I'm just trying one for now
    if ("inch") == 0)
        unit = Length::INCH;

    v = boost::any(unit);

// int main(int argc, char *argv[]) not included

And to spare including more code than what is necessary, I'm adding the option as follows:

po::options_description config("Configuration");
    ("to-unit", po::value<std::vector<Length::UnitType> >(), "The unit(s) of length to convert to")

If the compiler error is needed I can post it, but was hoping to keep the question simple looking. I've tried looking for examples, but the only other example I could really find was the examples/regex.cpp from the Boost website.

  1. Is the difference between my scenario and the examples found, except that mine is an Enum where the others are Structs? EDIT: My scenario did not require a custom validator overload.
  2. Is there a way to overload the validate method for an Enum? EDIT: Not needed.


In your case, you simply need to overload operator>> to extract a Length::Unit from an istream, as shown here:

#include <iostream>
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>

namespace Length

enum Unit {METER, INCH};

std::istream& operator>>(std::istream& in, Length::Unit& unit)
    std::string token;
    in >> token;
    if (token == "inch")
        unit = Length::INCH;
    else if (token == "meter")
        unit = Length::METER;
    return in;


typedef std::vector<Length::Unit> UnitList;

int main(int argc, char* argv[])
    UnitList units;

    namespace po = boost::program_options;
    po::options_description options("Program options");
             "The unit(s) of length to convert to")

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, options), vm);

    BOOST_FOREACH(Length::Unit unit, units)
        std::cout << unit << " ";
    std::cout << "\n";

    return 0;

A custom validator is not necessary.

Need Your Help

How do you find a needle in a haystack?

oop class-design program-structure

When implementing a needle search of a haystack in an object-oriented way, you essentially have three alternatives:

How do I create an array of strings in C?

c arrays string

I am trying to create an array of strings in C. If I use this code: