how to make a simple generic function... parameterized by
collection & value
Hi,
I'm trying to create a generic function in C++11 that takes a
collection
and a conversion function and returns a validation function that
performs the conversion and checks that the converted item is in the
collection.
Here's the supporting code:
// A Validator either returns a valid T or throws
template <class T> using Validator = T(*)(const std::string&);
// Example validator
std::string validate_string(const std::string &s)
{
if (s.length() == 0)
throw ValueError("Unacceptable empty String");
return s;
}
// Generic function to make a set validator
template <template<typename T> class C, typename T>
Validator<T> make_set_validator(const C<T> &valid_items,
Validator<T> validate)
{
return [&valid_items, &validate](const std::string &s)->T{
const T &x = validate(s);
if (std::find(std::begin(valid_items), std::end(valid_items),
x))
return x;
throw ValueError("Invalid item '" + s + "'");
};
}
And here's the call (that doesn't work):
std::unordered_set<std::string> on_off{"on", "off"};
Validator<std::string> on_off_validator =
make_set_validator(on_off, validate_string); // LINE 30: FAILS
try {
std::cout << on_off_validator("on") << "\n";
std::cout << on_off_validator("awf") << "\n";
} catch (ValueError &err) {
std::cout << err.what() << "\n";
}
GCC says:
g++ -o main.o -c -std=gnu++0x -pedantic -I/home/mark/opt/gcc470/
include main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:30:71: error: no matching function for call to
'make_set_validator(std::unordered_set<std::basic_string<char> >&,
std::string (&)(const string&))'
main.cpp:30:71: note: candidate is:
In file included from main.cpp:14:0:
optarg.hpp:64:14: note: template<template<class T> class C, class T> T
(* make_set_validator(const C<T>&, Validator<T>))(const string&)
optarg.hpp:64:14: note: template argument deduction/substitution
failed:
main.cpp:30:71: error: wrong number of template arguments (4, should
be 1)
In file included from main.cpp:14:0:
optarg.hpp:63:38: error: provided for 'template<class T> class C'
I've parameterized functions by collection type and value before using
template <template<typename T> class C, typename T>
but of course, there could be other problems.
Suggestions welcome:-)
Thanks!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]