So I have the following code:
enum class UnitType { yearType, monthType, dayType };
struct NoValidation {
void operator()(int x) const {}
};
template <UnitType U, typename Validator=NoValidation>
class Unit {
public:
explicit Unit(int v) : value(v) {
Validator()(v);
}
int query() const { return value; }
private:
int value;
};
auto validateYear = [](int x){
if(x < 0)
throw std::invalid_argument("Year is negative");
};
using Year = Unit<UnitType::yearType,decltype(validateYear)>;
using Month = Unit<UnitType::monthType>;
using Day = Unit<UnitType::dayType>;
The declaration of Month and Day works fine, but Year does not.
error: no matching constructor for initialization of '(lambda
Is there any way that I could modify this code to work with lambdas? Without passing the lambda as a parameter to the constructor, just as a template parameter. In theory a lambda is just syntactic sugar for a Functor, so why doesn't this work?
Edit I'd like to open this issue so that @Yakk can post his solution directly too my question.