问题描述
我试图通过想象神秘的结构更完全地抓住模板语法和语义。我相信以下语法是不允许的C ++ 11标准:
I am attempting to more fully grasp template syntax and semantics by imagining arcane constructs. I believe that the following syntax is not allowed by the C++11 standard:
template <typename T>
class A
{...};
// phony "specialization"
template <typename T>
class A<int>
{...};
但是,我不能在C ++ 11标准中找不到此语法。
However, I cannot find in the C++11 standard where this syntax is disallowed.
我是否更正了C ++ 11标准不允许显示的语法?如果是这样,可以在哪里找到不允许的语法?
Am I correct that the syntax shown is disallowed by the C++11 standard? If so, where can it be found that the syntax is disallowed?
推荐答案
我很惊讶,在14.5.5 [temp.class.spec]中说,必须在模板参数列表中使用类模板部分特化的所有模板参数。这将使 template< class T>因为
T
在模板参数列表< int>中未使用,所以类A< int& code>。
I'm quite surprised that there is no explicit statement in 14.5.5 [temp.class.spec] saying that all template parameters of a class template partial specialization must be used in the template-argument-list. That would make
template<class T> class A<int>
invalid because T
is not used in the template-argument-list <int>
.
我认为你的虚假专业化只是隐含地无效,因为你永远不能匹配它,所以它永远不能使用。如果你实例化
A< int>
那么匹配主模板。它不能匹配您的专业化,因为它有一个额外的模板参数 T
,无法推导(你建议可以通过说 A< int>< double>
但是这不是有效的C ++语法,所以没有帮助。)
I think your phony specialization is only implicitly invalid due to the fact that you can never match it, so it can never be used. If you instantiate
A<int>
then that matches the primary template. It can't match your specialization, because that has an additional template parameter, T
, which cannot be deduced (you suggest it could be provided by saying A<int><double>
but that is not valid C++ syntax, so doesn't help).
标准委员会澄清你的虚假专业化为什么是无效的(显然是,但我不能看到它说的是什么)。
I've asked the standards committee for clarification why your phony specialization is invalid (obviously it is, but I can't see where it says so).
这篇关于在C ++ 11标准中,它禁止'template< typename T> class A {...}; template< typename T> A类< int> {...}; (如果任何地方)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!