相关疑难解决方法(0)

为什么编译器不能从默认参数中推断出模板类型?

我很惊讶以下代码导致could not deduce template argument for T错误:

struct foo
{
  template <typename T>
  void bar(int a, T b = 0.0f)
  {
  }
};

int main()
{
  foo a;
  a.bar(5);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

调用a.bar<float>(5)解决了这个问题.为什么编译器不能从默认参数中推断出类型?

c++ templates default-arguments

18
推荐指数
2
解决办法
2354
查看次数

标签 统计

c++ ×1

default-arguments ×1

templates ×1