gpt4 book ai didi

c++ - 使用最终说明符专门化 C++ 类模板

转载 作者:太空狗 更新时间:2023-10-29 21:16:09 27 4
gpt4 key购买 nike

我有一个模板和一个特化定义如下:

template<typename T>
struct SomeTemplate final
{
};

template<>
struct SomeTemplate<int> final
{
};

int main()
{
SomeTemplate<int> test; // <-- error
}

使用 Visual C++ 2012 编译时出现以下错误:

error C2913: explicit specialization; 'SomeTemplate<T>' is not a specialization of a class template
with
[
T=int
]

它编译得很好并且在我删除时做正确的事情

  • final说明符
  • template<>来自专业

第一种情况对我来说有些意义,因为很可能是 final确实也限制了模板的特化(而不仅仅是继承)。第二种情况对我来说似乎有点奇怪,因为我认为这应该是一个语法错误。

这种行为是否正确?

最佳答案

我不确定这是不是 Visual C++ 2012 的错误,您的原始代码可以在较新版本的 VC++ 中正常编译 here .

  1. as it may very well be that final does also restrict specialization of templates (instead of only inheritance)

不,final specifier与模板特化无关。

Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from.

  1. The second case seems a bit strange to me, as I thought this should be a syntax error.

是的,它应该是一个语法 error如果你删除 template<> .

关于c++ - 使用最终说明符专门化 C++ 类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35887196/

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