quot;invalid use of incomplete typequot; error with partial template specialization(“不完整类型的无效使用部分模板特化错误)
问题描述
以下代码:
template <typename S, typename T>
struct foo {
void bar();
};
template <typename T>
void foo <int, T>::bar() {
}
给我错误
invalid use of incomplete type 'struct foo<int, T>'
declaration of 'struct foo<int, T>'
(我正在使用 gcc.)我的部分专业化语法错误吗?请注意,如果我删除第二个参数:
(I'm using gcc.) Is my syntax for partial specialization wrong? Note that if I remove the second argument:
template <typename S>
struct foo {
void bar();
};
template <>
void foo <int>::bar() {
}
然后它就可以正确编译了.
then it compiles correctly.
推荐答案
你不能部分特化一个函数.如果您希望在成员函数上这样做,则必须部分特化整个模板(是的,这很烦人).在大型模板化类上,要部分专门化一个函数,您需要一种解决方法.也许模板成员结构(例如 template )会起作用.或者,您可以尝试从另一个部分专门化的模板派生(如果您使用 this->member 表示法,则可以使用,否则您会遇到编译器错误).
You can't partially specialize a function. If you wish to do so on a member function, you must partially specialize the entire template (yes, it's irritating). On a large templated class, to partially specialize a function, you would need a workaround. Perhaps a templated member struct (e.g. template <typename U = T> struct Nested) would work. Or else you can try deriving from another template that partially specializes (works if you use the this->member notation, otherwise you will encounter compiler errors).
这篇关于“不完整类型的无效使用"部分模板特化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“不完整类型的无效使用"部分模板特化错误
基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
