小编Van*_*one的帖子

编译器选择错误的模板功能专业化

我拼命想让我的专业化工作,但仍然有不可编译的代码,因为推断出不正确的参数.请注意,错误不是关于定义模板,而是关于在错误的模板实现中应用不相关的操作.代码演示的问题的简化示例是:

struct Test { void Method() const {} };

template<typename T>
void Cmp(T _val) { _val > 1; }

template<>
void Cmp<const Test &>(const Test &_val) { _val.Method(); }

template<>
void Cmp<const char *>(const char *_val) { _val[2]; }

int main()
{
  Test test1;
  char test2[5];

  Cmp(10);    // ok, expected
  Cmp(test1); // error in Cmp(T)?! but expecting to instantiate Cmp(const Test &)
  Cmp(test2); // error in Cmp(T)?! but expecting to instantiate Cmp(const char *)
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我真的不希望使用像Cmp<const Test …

c++ templates

3
推荐指数
1
解决办法
1110
查看次数

标签 统计

c++ ×1

templates ×1