它適用於struct RS : public JV<T,1>
而不是struct RS : public JV<T,2>
轉換。無法從<括號內的初始化列表>
error: could not convert ‘{(0, j), (0, j)}’ from ‘<brace-enclosed initializer list>’ to ‘WJ<float>’
是否必須重載operator,()
? 代碼:
#include<iostream>
struct B {};
template <std::size_t... Is>
struct indices {};
template <std::size_t N, std::size_t... Is>
struct build_indices
: build_indices<N-1, N-1, Is...> {};
template <std::size_t... Is>
struct build_indices<0, Is...> : indices<Is...> {};
template<class T,int N>
struct JV {
JV(B& j) : JV(j, build_indices<N>{}) {}
template<std::size_t... Is>
JV(B& j, indices<Is...>) : jit(j), F{{(void(Is),j)...}} {}
B& jit;
T F[N];
};
template<class T>
struct RS : public JV<T,2>
{
RS(B& j): JV<T,2>(j) {}
};
template<class T>
struct WJ
{
WJ(B& j) {
std::cout << "WJ::WJ(B&)\n";
}
};
int main() {
B j;
RS<WJ<float> > b2(j);
}
'std :: array'不一定能做比C風格的數組更多的功能,只是初始化的行爲不同。 C風格的只需要一個對大括號,而'的std :: array'是聚合(從而使'{...}'初始化即使在C++ 03)。第二對大括號是內部C風格的數組成員。 – Xeo