gpt4 book ai didi

c++ - ...使用时没有模板参数错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:05 24 4
gpt4 key购买 nike

我再次需要你的帮助...

我有以下代码,它导致了在非命名空间范围内的显式特化错误:

namespace __test
{
template <int A, int B, typename C> class Test
{
template <int V> void check(C & a) { }
template <> void check<0>(C & a) { } //error: explicit specialization in non-namespace scope 'class __test::Test<A, B, C>'
};
}


因为我已经知道如何修复此类错误,所以我在类范围之外定义了特化,但是我遇到了另一个错误 - ... used without template parameters:

namespace __test
{
template <> void Test::check<0>(C & a) { } //error: 'template<int A, int B, class C> class __test::Test' used without template parameters
}


我可能只是愚蠢,但我不明白这个问题的原因,我不知道如何解决它...请帮忙!

最佳答案

根据我对标准的阅读,你想做的似乎是合法的。引用§14.7.3/18:

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. In such explicit specialization declaration, the keyword template followed by a template-parameter-list shall be provided instead of the template<> preceding the explicit specialization declaration of the member. The types of the template-parameters in the template-parameter-list shall be the same as those specified in the primary template definition.

因为您明确特化了一个成员函数模板而不是一个类成员模板,所以应该没问题;然而,Comeau、GCC 和 VC++ 都不允许以下语法,应该是正确的语法:

namespace test
{
template<int A, int B, typename C>
class Test
{
template<int V>
void check(C& a) { }
};

template<int A, int B, typename C>
template<>
void Test<A, B, C>::check<0>(C& a) { }
}
  • Comeau 说 error: a template declaration containing a template parameter list may not be followed by an explicit specialization declaration ,如果我们也将 §14.7.3/18 中的规则应用于成员函数模板,这就有意义了
  • GCC 说 invalid explicit specialization before '>' token ; enclosing class templates are not explicitly specialized ,如果我们将 §14.7.3/18 中的规则也应用于成员函数模板,这又是有意义的
  • VC++ 说 error C2768: 'test::Test<A,B,C>::check' : illegal use of explicit template arguments ,这不是有用的错误消息,但通常与其他错误消息一致

我的猜测 一定有一个缺陷报告归档,当封闭的类模板也没有显式特化时,它也不允许成员函数模板的显式特化;但是,我不能明确地说,因为 §14.7.3/18 的措辞在 C++03 标准和 C++0x FDIS 之间没有改变(如果针对 C++ 提交了 DR,它就会改变) 03 并接受)。

关于c++ - ...使用时没有模板参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com