相关疑难解决方法(0)

为什么不能在函数中声明模板?

阅读C++模板:完整指南,它说

请注意,模板不能在函数中声明

它不对书中的任何其他章节或外部资源给出解释和/或交叉引用.

有人可以帮忙解释一下.可能它会在本书后面解释,但还没有.如果先前解释过,我一定错过了它.

例:

int main()
{
  class DummyClass  //  This compiles ok
  {
    int object;
  };

  template <typename T> //  compile error "expected primary-expression before "template""
  class DummyTemplate
  {
    T object;
  };

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

我也不明白gcc的错误信息.错误消息说:

expected primary-expression before "template"
Run Code Online (Sandbox Code Playgroud)

c++ templates

23
推荐指数
2
解决办法
7673
查看次数

为什么我不能在函数内声明模板类型别名?

为什么我不能在函数内声明模板化类型别名?

#include <vector>

int main(){

    //type alias deceleration:
    template <typename T>
    using type = std::vector<T>;

    //type instantiation:
    type<int> t;

}
Run Code Online (Sandbox Code Playgroud)

错误:模板声明不能出现在块范围内

为什么我们被迫将这些声明置于块范围之外?

#include <vector>

//type alias deceleration:
template <typename T>
using type = std::vector<T>;

int main(){

    //type instantiation:
    type<int> t;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates typedef template-aliases c++14

8
推荐指数
1
解决办法
983
查看次数

标签 统计

c++ ×2

templates ×2

c++14 ×1

template-aliases ×1

typedef ×1