无法从<大括号括起来的初始值设定项列表转换>
could not convert from <brace-enclosed initializer list>
它适用于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);
}
如果要使用纯数组F{(void(Is),j)...}
,则需要删除额外的{}
。或者像你说的那样把它改成std::array<T, N> F
。
然而,普通数组只是使用 {}
进行初始化,std::array
是一个包含数组的聚合,因此它使用双大括号。
请参阅将 std::array 与初始化列表一起使用以获得更好的解释。
您的问题是多了一对{}
大括号。 改变
JV(B& j, indices<Is...>) : jit(j), F{{(void(Is),j)...}} {}
自
JV(B& j, indices<Is...>) : jit(j), F{(void(Is),j)...} {}
它工作正常。
它与std::array
一起使用的原因是array
是一个包含实际数组的聚合:
// from 23.3.2.1 [array.overview]
namespace std {
template<typename T, int N>
struct array {
...
T elems[N]; // exposition only
因此,要初始化array
,与初始化实际数组相比,需要额外的一对大括号。 GCC 允许您省略多余的大括号,但抱怨:
std::array<int, 3>{1, 2, 3};
警告:'std::array::value_type [3] {aka int [3]}' [-Wmissing-braces] 的初始值设定项周围缺少大括号
替换
T F[N];
跟
std::array<T,N> F;
成功了!似乎std::array
可以做的比 C 数组更多。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- EASTL矢量<向量<int>>连续的
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改