C 11:此不完整类型错误的解决方法
C++11: Workaround Use Of This Incomplete Type Error?
#include <iostream>
#include <array>
using namespace std;
constexpr int N = 10;
constexpr int f(int x) { return x*2; }
typedef array<int, N> A;
template<int... i> struct F { constexpr A f() { return A{{ f(i)... }}; } };
template<class X, class Y> struct C;
template<int... i, int... j>
struct C<F<i...>, F<j...>> : F<i..., (sizeof...(i)+j)...> {};
template<int n> struct S : C<S<n/2>, S<n-n/2>> {}; // <--- HERE
template<> struct S<1> : F<0> {};
constexpr auto X = S<N>::f();
int main()
{
cout << X[3] << endl;
}
我得到了:
test.cpp:15:24: error: invalid use of incomplete type ‘struct C<S<5>, S<5> >’
我怀疑这是因为S的定义将自己用作基类。(正确?)
解决此问题的最佳方法是什么?
更新:
这是固定版本:
#include <iostream>
#include <array>
using namespace std;
constexpr int N = 10;
constexpr int f(int x) { return x*2; }
typedef array<int, N> A;
template<int... i> struct F { static constexpr A f() { return A{{ ::f(i)... }}; } };
template<class A, class B> struct C {};
template<int... i, int... j> struct C<F<i...>, F<j...>> : F<i..., (sizeof...(i)+j)...>
{
using T = F<i..., (sizeof...(i)+j)...>;
};
template<int n> struct S : C<typename S<n/2>::T, typename S<n-n/2>::T> {};
template<> struct S<1> : F<0> { using T = F<0>; };
constexpr auto X = S<N>::f();
int main()
{
cout << X[3] << endl;
}
定义 C
而不仅仅是声明。
template<class X, class Y> struct C {};
在您使用的地方,部分专业化不匹配,并且主模板是实例化的,只是声明。
您可能想知道为什么不考虑这种专业:专业不考虑转换,而只是静态类型。这就是为什么他们如此勇敢地与继承不相容。
您可以委派S::f
而不是使用继承?
template<int n> struct S {
constexpr A f() { return C<S<n/2>, S<n-n/2>>::f(); }
};
相关文章:
- 我的模板类方法返回错误类型?
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- glDrawElements GL 错误类型 = 0x824c,严重性 = 0x9146,消息 = GL_INVALID
- Eclipse/CDT_C++给出"语义错误_"类型XXX无法解决"。项目运行
- turbo 为什么我的C++代码中出现错误类型名称预期"错误?
- "auto"推断出 hashtable_policy.h 中的错误类型
- 设计自己的结果/两者之一:是否应该始终指定成功类型和错误类型
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 在 vscode 上编译错误.类型说明符是 C++11 扩展
- 不允许使用错误类型
- 当读取错误类型时,CIN覆盖我的初始化值
- C++:"错误:类型'const char*'和'const char [28]'的操作数无效到二进制'ope
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 在shared_from_this()中的错误类型(在继承类中)(是否有dyn.type-ware shared Poi
- 为什么缩小转换范围不能防止错误类型的map.insert()失败
- 程序总是返回二进制'>>':找不到运算符,它采用错误类型的左操作数
- 错误:类型为 '.&' 来自类型的临时 '..*康斯特'
- 涉及将要馈送的向量转换为execvp的错误类型
- C++中是否存在错误类型