递归嵌套初始化列表采用变体
我希望一个对象 obj
可以从Pairs的 initializer_list
初始化。但是,这对的第二个值是bool,int和 obj
的变体。 GCC报告了我猜找到正确的构造函数的麻烦。我可以为以下代码递归制作这项工作吗?
#include <utility>
#include <string>
#include <variant>
struct obj;
using val = std::variant<int, bool, obj>;
struct obj
{
obj(std::initializer_list<std::pair<std::string, val>> init) {
}
};
int main()
{
obj O = { {"level1_1", true }, { "level1_2", 1 }, { {"level2_1", 2}, {"level2_2", true}}};
}
GCC 12.1不明白:
<source>: In function 'int main()':
<source>:57:93: error: could not convert '{{"level1_1", true}, {"level1_2", 1}, {{"level2_1", 2}, {"level2_2", true}}}' from '<brace-enclosed initializer list>' to 'obj'
57 | obj O = { {"level1_1", true }, { "level1_2", 1 }, { {"level2_1", 2}, {"level2_2", true}}};
| ^
| |
|
有什么暗示我需要更改的内容?
编辑:似乎使用初始化器列表的方法可能不合适。也许使用模板存在解决方案,这也是值得赞赏的。我知道从 nlohmanns json library (请参阅“ JSON as json as First-class数据类型”)) 。此外,解决方案应在初始化过程中不使用堆库来实现。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用模板元编程在编译时创建特定类型。
在上面的代码中,
obj&lt; 2&gt; :: value
将被编译为:我相信不应该进行堆分配,因为您仅使用基本类型(bool,int),
std :: string_view ,
std :: pair
和std :: variant
(应该像结构一样行为),std:prinisterizer_list 。检查此处用于演示上面示例中的所有
o
值在堆栈上。You could use template metaprogramming to create specific types at compile time.
In the code above,
obj<2>::value
would be compiled to:I believe no heap allocations should be made, since you are only making use of basic types (bool, int),
std::string_view
,std::pair
andstd::variant
(should behave like a struct), andstd:initializer_list
. Check here for a demo showing all theo
values in the example above are on the stack.正如Igor指出的那样,如果没有对您的类型或构造函数进行任何更改,您的代码可以通过为您的子对象指定的显式类型进行编译:
我相信问题可能源于
std :: Pair
,,以及包含不完整类型的变体。我们可以用我们自己的自定义类型替换std :: pair&lt; ...&gt;
缺乏任何内部存储的类型:这样,我们得到了您所需的语法,但缺乏任何实现。我的猜测是,问题在于
std :: Pair
与std :: variant
结合使用。我们可以通过拥有kv_pair
来恢复我们的存储空间,而是通过成对的字符串和指针的便利包装器,即为完整而完整的实现:
As Igor noted, without any changes to your type or constructor, your code can compile fine with an explicit type specified for your subobject:
I believe the problem may originate with the particular combination of
std::pair
, along with a variant that contains an incomplete type. We can observe this by replacingstd::pair<...>
with our own custom type that lacks any internal storage:With this, we get your desired syntax, but lack any implementation. My guess here is that the problem lies with
std::pair
in combination withstd::variant
. We can get back our storage by havingkv_pair
instead be a convenience wrapper over pairs of strings and pointers, i.e.So for a full and complete implementation: