阅读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) 为什么我不能在函数内声明模板化类型别名?
#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)