c ++模板 - (类型推导编译时错误)

时间:2015-05-27 15:08:18

标签: c++ templates

我试图实现对xml阅读器的解析,我有一个函数或一组函数实现从de xml文件中获取de值所需的解析并设置变量,我已经实现了几个模板化函数泛型使用,但我陷入了编译错误,编译器正试图替换所有在方法中加上语法化的函数并生成编译时错误(类型推导)。我试图通过说明类型向编译器表明每个分支是不同的情况,但是不起作用,这里是代码:

#include <string>
#include <stdexcept>
#include <sstream>
namespace
{
  int cantidad_repeticiones;
  int execution_code;
  bool should_report;
  bool time_stamp;
  std::string cmd_description;
  int cmd_id;
  unsigned delay_entre_comandos;

  void alpha_to_bool(bool *aBool,const std::string & aString)
  {
    std::istringstream(aString) >> std::boolalpha >> (*aBool);
  }


  template<typename T>
  void convertoToNumber( T * aNumber, const std::string & aString)
  {
    std::stringstream  mStringstream(aString);
    mStringstream >>  (*aNumber);
  }
  template<typename T>
  void set_option(T * aValuePtr,const char * xml_type,const char * xml_value )
  {
    std::string type(xml_type);
    std::string aValue(xml_value);
    if(type=="float") convertoToNumber(aValuePtr,aValue);
    if(type=="bool") alpha_to_bool(aValuePtr,aValue);
    if(type=="int") convertoToNumber(aValuePtr,aValue);
    if(type=="unsigned") convertoToNumber(aValuePtr,aValue);
    if(type=="double") convertoToNumber(aValuePtr,aValue);
  }
  void parse_xml_option(const char * xml_option,const char * xml_type,const char * xml_value)
  {
    std::string string_cache(xml_option);
    if(string_cache=="timestamp") set_option(&time_stamp,xml_type,xml_value);
    if(string_cache=="repeticiones") set_option(&cantidad_repeticiones,xml_type,xml_value);
    if(string_cache=="delay_entre_comandos") set_option(&delay_entre_comandos,xml_type,xml_value);
    if(string_cache=="generate_report") set_option(&should_report,xml_type,xml_value);

  }
}
int main()
{


return 0;
}

代码不是强制性的,我不猜测为什么,是否有任何方式来指示编译器代码的每个分支是不同的情况,并且它不能尝试推断所有情况的类型? Thx提前

另外,我试图向编制者指出类型,例如:

if(type=="float") convertoToNumber<float>(aValuePtr,aValue);

它的动作会产生更多的编译错误。 编译出:

cannot convert 'int*' to 'bool*' for argument '1' to 'void {anonymous}::alpha_to_bool(bool*, const string&)'
note:   no known conversion for argument 1 from 'bool' to 'bool&'

表示该行:

 if(type=="bool") alpha_to_bool(aValuePtr,aValue);

有错误

3 个答案:

答案 0 :(得分:1)

无论T是什么,当编译器编译set_option<T>时,它必须编译表达式alpha_to_bool(aValuePtr, aValue)

当然,当T不是bool时,这种情况很糟糕,因此您对set_option的一半调用可能无法正常工作,因为它们会传递指针bool以外的其他类型。

你应该重载 set_option或其他类似方法:例如

void set_option(bool * aValuePtr,const char * xml_type,const char * xml_value )
{
  std::string type(xml_type);
  std::string aValue(xml_value);
  if(type=="bool") {
    alpha_to_bool(aValuePtr,aValue);
  } else {
    throw std::runtime_error("Attempted to assign " + type + " to a `bool'");
  }
}

,其余类型也是如此。

答案 1 :(得分:0)

我解决了这个问题: 这条线是问题所在:

if(type=="bool") alpha_to_bool(aValuePtr,aValue);

我换了这个:

if(type=="bool") alpha_to_bool(reinterpret_cast<bool *>(aValuePtr),aValue);

它编译和工作,但我仍然不明白为什么编译器试图推断错误的类型.......

答案 2 :(得分:0)

此程序中的所有函数都是模板函数,alpha_to_bool除外。这意味着当从隐式特化alpha_to_bool调用set_option<int>时,它被调用int *作为其参数,但它当然需要一个bool *