0753c65c   
   From: daniel.kruegler@googlemail.com   
      
   Am 12.03.2012 02:46, schrieb Mark Summerfield:   
   > 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 using Validator = T(*)(const std::string&);   
      
   OK, this defines an alias template Validator which maps to a function   
   pointer type accepting a std::string and returning a T.   
      
   > // 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 class C, typename T>   
   > Validator make_set_validator(const C &valid_items,   
   > Validator 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 + "'");   
   > };   
   > }   
      
   Here is the problem: Your function template make_set_validator should   
   return function pointer (according to the return type specification),   
   but it does instead return a lambda closure, which is a class type with   
   a function call operator overload. The important point to note here is   
   that this lambda closure has captures and this excludes it from being   
   stateless. Without the captures there would be the requirement that the   
   closure type provides a conversion function that would match your   
   constraints, but alas due to the captures this is not possible. Your   
   function should return std::function instead.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|