c++ - 函数模板重载(不同数据类型验证函数作为参数)

标签 c++ validation templates c++11 function-templates

在当前状态下,程序验证数据类型的输入:int 和 string。

//valid score range between 0 and 100
bool validScore(int value) 
{
  if(value < 0 || value > 100)
      return false;
  return true;
}
//valid name composed of letters
bool validName(string word)
{
  for(int i = 0; i < word.length(); i++)
  {
    if(!isalpha(word[i]) )
        return false;
  }
  return true;
}

输入是通过每个数据类型的两个函数获得的:get_int() 和 get_word()。

验证函数是可选参数,适用于未指定 int 范围且名称可以包含任何字符的情况。

bool get_int(const string&, int&, bool(*validScore)(int) = 0);
bool get_word(const string&, string&, bool(*validWord)(const string) = 0);

// bool get_int(const string& prompt, int&n, (*validScore)(int)) 
bool get_word(const string& prompt, string&word, bool(*validWord)(const string) )
{
  while(1)
  {
    string line;
    cout << prompt;

    if(!getline(cin, line) 
      break;

    istringstream iss(line);

    if(iss >> word) // iss >> n
    {
      // validScore == 0 || validScore(n)
      if(validWord == 0 || validWord(word)) 
        return true;
    }
  }
  return false;
}

我想知道,如果可能的话,如何正确声明一个函数模板,以便在需要验证时简化流程。

template<typename T> bool get_item(const string&, T&, ???);

使用不同的验证函数重载函数模板 get_item 是否是一个潜在的解决方案?

最佳答案

如果你真的喜欢函数指针,那么

template<typename T> bool get_item(const string&, T&, bool (*)(const T&) = nullptr);

template<typename T> bool get_item(const string&, T&, bool (*)(T) = nullptr);

匹配您的 valid* 函数的现有签名(请注意,这会产生拷贝)。

你也可以让它完全通用:

template<typename T, typename F> bool get_item(const string&, T&, F);

要处理不需要验证的情况,可以使用单独的重载:

template<typename T> bool get_item(const string &s, T &t){
    return get_item(s, t, [](const T&){return true;});
}

关于c++ - 函数模板重载(不同数据类型验证函数作为参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30719405/

相关文章:

php - Codeigniter 表单验证...表单重新填充

C++11:为什么这个范围循环将 FPS 降低 35?

c++ - 多边形顶点减少库保持几乎相同的形状

c++ - GTK+/Gtkmm,检查notebook标签页是否已经存在,不存在才创建

javascript - node.js 中 Jade 模板的全局变量

c++ - 默认模板参数的语义检查

c++ - 使用 clang-format 3.6 的 cpp 模板 <> 间距未按预期工作

c++ - 从二进制文件中读取对象

Java验证约束: How to send validation error to client

jquery - MVC4 不显眼的验证不等待远程验证完成