C ++模板返回参数

时间:2015-02-08 18:30:01

标签: c++ templates

我想要一个只返回模板类型的函数,但不接受任何参数。 但是我的

template <typename T>
T getSomeT() {
  T some;
  return some;
}

没有用,它说&#34; error: no matching function for call to 'getSomeT'&#34;和&#34; note: candidate template ignored: couldn't infer template argument 'T'&#34;

但如果我在其中添加一些伪模板参数,它的工作正常:

template <typename T>
T getSomeT(T fake) {
  T some;
  return some;
}

1 个答案:

答案 0 :(得分:7)

正如错误告诉您的那样,&#34; couldn't infer template argument 'T'&#34;。

告诉它:

int result = getSomeT<int>();