C ++ 11:解决方法使用此不完整的类型错误? [英] C++11: Workaround Use Of This Incomplete Type Error?
本文介绍了C ++ 11:解决方法使用此不完整的类型错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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的定义将自身用作基类.(正确吗?)
I suspect this is because the definition of S is using itself as a base class. (Correct?)
解决此问题的最佳方法是什么?
What is the best way to fix this?
更新:
这是固定版本:
#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
而不是仅仅声明它.
Define C
instead of just declaring it.
template<class X, class Y> struct C {};
在使用它的地方,部分专业化不匹配,并且主模板被实例化,这只是一个声明.
In the place you use it the partial specialization does not match and the primary template is instantiated, which is just a declaration.
您可能想知道为什么不考虑专业化:专业化不考虑转换,而只考虑静态类型.这就是为什么它们与继承是如此背叛的原因.
You may wonder why that specialization is not considered: specializations don't consider conversions, but just the static type. That's why they are so treacherously incompatible with inheritance.
这篇关于C ++ 11:解决方法使用此不完整的类型错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文