M
Mark
Hi,
I am trying to make a function that will create and return another
function (a closure that captures some state).
Here's the usage I want:
std::vector<std::string> on_off{"on", "off"};
StringValidatorFunction on_off_validator =
make_string_set_validator(on_off.begin(), on_off.end());
auto s = on_off_validator("on"); // s == "on"
auto t = on_off_validator("awf"); // throws
std::vector<std::string> color{"red", "green", "blue"};
StringValidatorFunction color_validator =
make_string_set_validator(color.begin(), color.end());
auto r = color_validator("red"); // s == "red"
auto y = color_validator("yellow"); // throws
Here's what I've got so far:
using StringValidatorFunction = std::function<std::string(const
std::string&)>;
template <class Iter>
StringValidatorFunction make_string_set_validator(const Iter first,
const Iter end)
{
std::unordered_set<std::string> valid_strings;
std::for_each(first.begin(), first.end(),
[&valid_strings](const std::string &s)
{valid_strings.insert(s);});
return [valid_strings](const std::string &s)->std::string{
if (valid_strings.find(s) == valid_strings.end())
throw ValueError("Invalid string '" + s + "'");
return s;
};
}
This is the first time I've tried to use lambdas (I'm just starting
out with C++11), and GCC 4.7.0 isn't helpful (at least not to me):
main.o: In function `main':
main.cpp.text+0x253): undefined reference to
`std::function<std::string ()(std::string const&)>
make_string_set_validator<__gnu_cxx::__normal_iterator<std::string*,
__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string,
std::allocator<std::string> > >)'
collect2: error: ld returned 1 exit status
Any suggestions welcome
Thanks!
I am trying to make a function that will create and return another
function (a closure that captures some state).
Here's the usage I want:
std::vector<std::string> on_off{"on", "off"};
StringValidatorFunction on_off_validator =
make_string_set_validator(on_off.begin(), on_off.end());
auto s = on_off_validator("on"); // s == "on"
auto t = on_off_validator("awf"); // throws
std::vector<std::string> color{"red", "green", "blue"};
StringValidatorFunction color_validator =
make_string_set_validator(color.begin(), color.end());
auto r = color_validator("red"); // s == "red"
auto y = color_validator("yellow"); // throws
Here's what I've got so far:
using StringValidatorFunction = std::function<std::string(const
std::string&)>;
template <class Iter>
StringValidatorFunction make_string_set_validator(const Iter first,
const Iter end)
{
std::unordered_set<std::string> valid_strings;
std::for_each(first.begin(), first.end(),
[&valid_strings](const std::string &s)
{valid_strings.insert(s);});
return [valid_strings](const std::string &s)->std::string{
if (valid_strings.find(s) == valid_strings.end())
throw ValueError("Invalid string '" + s + "'");
return s;
};
}
This is the first time I've tried to use lambdas (I'm just starting
out with C++11), and GCC 4.7.0 isn't helpful (at least not to me):
main.o: In function `main':
main.cpp.text+0x253): undefined reference to
`std::function<std::string ()(std::string const&)>
make_string_set_validator<__gnu_cxx::__normal_iterator<std::string*,
std::allocator<std::string> > >,std::vector said:(__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string,
__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string,
std::allocator<std::string> > >)'
collect2: error: ld returned 1 exit status
Any suggestions welcome
Thanks!