How to declare a "base" type for a generic std :: function argument

I am trying to write a general wrapper (for some script interpreter) for a function / class method that converts all call parameters from a string to an arbitrary type T. I will try to highlight the topic in points:

  • Script allows you to display a user function
  • When the interpreter tries to process the user-defined function, the callback procedure is executed
  • The callback is intended to receive an array of objects that describe (one at a time) the values ​​of the arguments
  • I already have (template) routines that convert a string to an arbitrary (basic) type T
  • I would like to wrap a user procedure (provided externally as a varadic std :: function <> type) so that the conversion from subsequent lines from the callback array to the corresponding argument is done automatically

Example:

The prototype for the callback procedure is as follows:

int CallbackFn(Interp *interp, int argc, const char **argv)

I got a (sample) custom function:

int UserRoutine(const std::string &in_str, int x);

so std :: function will look like this:

std::function<int(const std::string&, int)>

The general conversion procedure has the syntax:

template <typename T>
T conv(const char *str);

I have specializations that convert:

  • "const char *" to "std :: string"
  • "const char *" to "int"

so that ideally the conversion would look like this:

std::string p0 = conv<std::string>(argv[0]);
int p1 = conv<int>(argv[1]);

, std:: function <... > , - . const T &, "" T.

, ?

+4
1

, , . - operator>> (istream&, T&). std::stringstream. , , operator<< .

, . std::function<int(const std::string&, int)>. std::function<std::string(std::string)>. .

template<typename RET, typename Args...>
std::function<std::string(std::string) (RET (*fptr)(Args...));

:

std::tuple<Args...> args;
std::istringstream iss(fromScript);
iss>>args;
std::ostringstream oss;
oss << *fptr(args.get<0>, args.get<1>(), ...);
return oss.str();

*fptr . ++ 11: , ?

[] , : " , , , const T&, " " T.". , std::decay<Arg>.

+4

Source: https://habr.com/ru/post/1695245/


All Articles