无法从<用括号括起来的初始化程序列表>转换。 [英] could not convert from <brace-enclosed initializer list>
问题描述
它对 struct RS:public JV< T,1>
有效,但不适用于 struct RS:public JV< T,2> code>。
It works for struct RS : public JV<T,1>
but not for struct RS : public JV<T,2>
.
error: could not convert ‘{(0, j), (0, j)}’ from ‘<brace-enclosed initializer list>’ to ‘WJ<float>’
我是否必须重载 operator,()
?
代码:
Do I have to overload operator,()
?
Code:
#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);
}
推荐答案
您需要删除多余的内容 {}
如果要使用纯数组 F {(void(Is),j)...}
。或将其更改为 std :: array< T,N> F
。
You need to remove the extra {}
if you want to use a plain array F{(void(Is),j)...}
. Or change it to std::array<T, N> F
like you said.
普通数组仅使用 {}
进行初始化,但是 std :: array
是一个包含数组的聚合,因此它使用双括号。
A plain array simply uses {}
for initialization, however and std::array
is an aggregate which contains an array, so it uses the double braces.
请参见使用带有初始化列表的std :: array 以获得更好的解释。
See Using std::array with initialization lists for a better explanation.
这篇关于无法从<用括号括起来的初始化程序列表>转换。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!