为什么我可以部分专业化但不能完全专门化成员模板? [英] Why can I partially specialize but can't fully specialize member templates?
问题描述
这个有效:
This works:
template <class T>
class A
{
template<typename Y, typename Z>
struct C{
void operator()(int x);
};
template<typename Z>
struct C<int, Z>{
void operator()(int x);
};
};
template <class T>
template <typename Z>
void A<T>::C<int, Z>::operator()(int x){
}
但这不是:
But this doesn't:
template <class T>
class A
{
template<typename Y>
struct C{
void operator()(int x);
};
template<>
struct C<int>{
void operator()(int x);
};
};
template <class T>
template <>
void A<T>::C<int>::operator()(int x){
}
我当然不会指望这种行为,因为你可以很容易地创建一个假人模板并将一个完全专业化的模板转换为部分专业模板。但令我感到困惑的是,这也是Clang中观察到的行为。
I certainly wouldn't expect this behavior, because you can easily create a dummy template and convert a fully specialized template into a partially specialized template. But what got me really confused is that, this is the observed behavior in Clang too.
有人可以解释这背后的原因吗?
Can anyone explain the reasoning behind this please?
推荐答案
On 6/6/2018下午8:37,TalhaSaruhan写道:
On 6/6/2018 8:37 PM, TalhaSaruhan wrote:
为什么我可以部分专业但不能完全专门化成员模板?
Why can I partially specialize but can't fully specialize member templates?
因为标准是这样说的:
[temp.expl.spec] / 16在类模板成员或命名空间作用域中出现的成员模板的显式特化声明中,成员模板及其一些封闭类模板可能仍然是非专业化的,除了
声明如果其封闭的类模板也没有明确专门化,则不应明确专门化类成员模板。
[temp.expl.spec]/16 In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.
强调我的。现在,坦率地说,我从未理解为什么标准放置此限制。
Emphasis mine. Now, frankly, I never understood why the standard places this restriction.
这篇关于为什么我可以部分专业化但不能完全专门化成员模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!