相关疑难解决方法(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
查看次数

默认模板类型可以是通用引用吗?

在下面,是&&一个普遍的参考?

template <class Function = std::greater<int> > void f(Function&& f = Function());
Run Code Online (Sandbox Code Playgroud)

c++ templates reference c++11 universal-reference

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