2012-10-27 71 views
1
#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; 
} 

我越来越:C++ 11:解决方法使用此不完整类型错误?

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; 
} 
+0

相关:http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth –

+0

犯规编译。即使C++ 11也不允许在很多地方使用省略号。 –

+0

@ööTiib:我知道它不能编译,我显示编译器错误。它与椭圆无关。 –

回答

2

定义C,而不是仅仅宣布它。

template<class X, class Y> struct C {}; 

在你使用它的偏特不匹配的地方, 主模板实例,这只是一个声明。

您可能想知道为什么没有考虑专业化:专业化不考虑转化,而只是静态类型。这就是为什么他们与继承非常不相容。

+0

嗯,这很糟糕。不管怎么说,还是要谢谢你。 –

+0

@ AndrewTomazos-Fathomling我冥想了你在这里想要做的事情,但对我来说没有多大意义。该代码实际上试图实现什么?我可以弄清楚'F'和'C',但是'S'的目的不在我身上。 – pmr

+0

请参阅:http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth –

0

你可以委托S::f而不是继承吗?

template<int n> struct S { 
    constexpr A f() { return C<S<n/2>, S<n-n/2>>::f(); } 
}; 
+0

'C ,S >'需要绑定到专业化'C ,F >',这不会因为模板专业化不考虑继承。 –

相关问题