在 C++ 模板类中,我可以使用相同的名称对模板参数进行 typedef 吗? [英] In a C++ template class, can I typedef the template parameter using the same name?
本文介绍了在 C++ 模板类中,我可以使用相同的名称对模板参数进行 typedef 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个模板类:
template<typename Layout>
class LayoutHandler : Handler {
};
并且我想将参数 Layout 公开给类的用户.然后:
and I want to expose the parameter Layout to the user of the class. Then:
template<typename Layout>
class LayoutHandler : Handler {
public:
typedef Layout Layout; // using the same name
};
VS2012 可以编译这段代码,并给出预期的结果.(我使用 std::is_same 来检查它.)这在标准 C++03 或 C++11 中是否允许?
VS2012 can compile this code, and give the expected result. (I use std::is_same to check it.) Is this allowed in standard C++03 or C++11?
推荐答案
在 C++11 中是不允许的.
It is not allowed in C++11.
typedef
是一个声明.(见第 7.1.3 节)
A typedef
is a declaration. (see section 7.1.3)
template
参数不能在其作用域(包括嵌套作用域)内重新声明.(见第 14.6.1.6 节)
A template
parameter can't be redeclared within its scope (including nested scopes). (see section 14.6.1.6)
这篇关于在 C++ 模板类中,我可以使用相同的名称对模板参数进行 typedef 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文