Re: how to make a simple generic function... parameterized by
collection & value
Hi Daniel,
I tried what you suggested but got the same errors.
template<typename T>
struct identity { typedef T type; };
template<typename T>
using NonDeduced = typename identity<T>::type;
template<typename T>
using Validator = std::function<T(const std::string)>;
template<typename C>
Validator<typename C::value_type>
makeSetValidator(const C &validItems,
NonDeduced<typename C::value_type(*)(const std::string)> validate)
{
return [=](const std::string s)->typename C::value_type{
const auto x = validate(s);
if (std::find(std::begin(validItems), std::end(validItems), x)
!= std::end(validItems))
return x;
throw ValueError("Invalid item '" + s + "'");
};
}
template<typename T>
T validate(const std::string s) { ... }
At the call site I tried:
auto v = makeSetValidator<int>(std::set<int>{-4, 8, 31},
validate<int>);
auto v = makeSetValidator(std::set<int>{-4, 8, 31}, validate<int>);
auto v = makeSetValidator<int>(std::set<int>{-4, 8, 31}, validate);
auto v = makeSetValidator(std::set<int>{-4, 8, 31}, validate);
None of which would compile.
I think I'll give up now:-)
Thanks!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]