不完整类型(类方法专用化)的使用无效
Invalid use of incomplete type (class method specialization)
首先,我已经阅读了许多其他问题,但找不到解决方案。 因此,在将其标记为重复项之前,请确保重复回答问题。
我正在尝试专门针对类C2
F::operator()
;但是,C2
有一个模板参数,我希望F::operator()
对所有C2
的行为都相同。
编译器错误:
error: invalid use of incomplete type ‘struct F<C2<T> >’
void F<C2<T>>::operator()()
另外,我没有Handle& h
,而是尝试Handle* h
并收到相同的错误。
#include<iostream>
struct C1
{
void foo()
{
std::cout << "C1 called" << std::endl;
}
};
template<typename T>
struct C2
{
void bar();
};
template<>
void C2<int>::bar()
{
std::cout << "C2<int> called" << std::endl;
}
template<typename Handle>
struct F
{
F(Handle& h_) : h(h_) {}
void operator()();
Handle& h;
};
template<>
void F<C1>::operator()()
{
h.foo();
}
template<typename T>
void F<C2<T>>::operator()()
{
h.bar();
}
int main()
{
C1 c1;
F<C1> f_c1 (c1);
f_c1();
C2<int> c2;
F<C2<int>> f_c2 (c2);
f_c2();
}
没有像成员函数的部分特化这样的事情。您需要首先对整个类进行部分专业化:
template <typename T>
struct F<C2<T>>
{
void operator()();
};
template <typename T>
void F<C2<T>>::operator()() {}
由于这是一个重量级的解决方案,或者,您可以利用标签调度:
template <typename T> struct tag {};
template <typename Handle>
struct F
{
F(Handle& h_) : h(h_) {}
void operator()()
{
call(tag<Handle>{});
}
private:
void call(tag<C1>)
{
h.foo();
}
template <typename T>
void call(tag<C2<T>>)
{
h.bar();
}
Handle& h;
};
演示
相关文章:
- .cpp和.h文件中的模板专用化声明
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 无法访问嵌套类.类的使用无效
- 调用专用模板时出错"no matching function for call to [...]"
- 模板专用化(按容器):value_type
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- FFMPEG配置文件级别id大小无效
- 错误:从"int"到枚举c++的转换无效
- 如何修复此错误:className::className的无效使用
- 静态数据成员模板专用化的实例化点在哪里
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- C++-模板嵌套类的引用初始化无效
- 特征 3 类的模板专用化
- C++ 在模板专用化中从 strcmp() 中的 'char' 到 'const char*' 的无效转换
- 不完整类型(类方法专用化)的使用无效
- 别名模板、部分专用化和无效参数类型无效
- 部分模板专用化 c++ 未完全使用不完整类型无效
- C++模板专用化友元迭代器错误:未使用不完整的类型无效