@SPeak 感谢大佬,已经解决了。不过我还是很疑惑,vector练习里也有自赋值的检测,为什么可以正常进入练习,也只是提示double free,为什么循环链表这里会出现死循环
dustchens
-
dsx自动检测出现错误 -
dsx自动检测出现错误@SPeak 大概定位到了问题,在我当前的代码下,slist.2.cpp里的 // d2ds::SLinkedList<int> intList2(intList1); 这些拷贝语句都会使得无法进入该练习
https://github.com/dustchens/my-d2ds/tree/main/dslings/exercises/linked-list
这是我出错的仓库链接 -
dsx自动检测出现错误@SPeak 乱码确实没有了。
是运行下一个检查没有成功,是否需要改slist.2.cpp里的内容?还是我把 .gitignore删掉上传全部内容?没有用过xmake,是不是我自己的代码出现了问题导致的这后面的运行不起来。。 我在修改slist.2.cpp里的内容,看看注释掉一部分后哪里有问题,定位到导致出错的语句,然后贴上自己SLinkedList.hpp里的代码,这样可以吗? -
dsx自动检测出现错误删除main之前在 .xlings里执行命令的完整输出
xmake xlings -D --project=. J:\cpp_project\d2ds d2x checker ✅ Successfully ran dslings\tests\dslings.0.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- 🌏Progress: [===>----------------------------------------------] 3/49 [Target: 0-dslings-B-1] - normal ✅ Successfully ran dslings\tests\dslings.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | mVal.get() == 2 (2 == 2) 🌏Progress: [====>---------------------------------------------] 4/49 [Target: 0-dslings-B-2] - normal ✅ Successfully ran dslings\tests\dslings.2.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | mVal.get() == 2 (2 == 2) [D2DS LOGI]: - ✅ | mVal.get() == 2 (2 == 2) 🌏Progress: [=====>--------------------------------------------] 5/49 [Target: 1-array-A-0] - normal ✅ Successfully ran dslings\useage\array\array.u0.cpp 🎉 The code is compiling! 🎉 🌏Progress: [======>-------------------------------------------] 6/49 [Target: 1-array-B-0] - normal ✅ Successfully ran dslings\tests\array\array.0.cpp 🎉 The code is compiling! 🎉 🌏Progress: [=======>------------------------------------------] 7/49 [Target: 1-array-B-1] - normal ✅ Successfully ran dslings\tests\array\array.1.cpp 🎉 The code is compiling! 🎉 🌏Progress: [========>-----------------------------------------] 8/49 [Target: 1-array-B-2] - normal ✅ Successfully ran dslings\tests\array\array.2.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | BigFiveTest::destructor() [D2DS LOGI]: - ✅ | BigFiveTest::copy_assignment() [D2DS LOGI]: - ✅ | BigFiveTest::move_assignment() 🌏Progress: [=========>----------------------------------------] 9/49 [Target: 1-array-B-3] - normal ✅ Successfully ran dslings\tests\array\array.3.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | intArr[0] == 5 (5 == 5) 🌏Progress: [==========>---------------------------------------] 10/49 [Target: 1-array-B-4] - normal ✅ Successfully ran dslings\tests\array\array.4.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | i == intArr[i] (0 == 0) [D2DS LOGI]: - ✅ | i == intArr[i] (1 == 1) [D2DS LOGI]: - ✅ | i == intArr[i] (2 == 2) [D2DS LOGI]: - ✅ | i == intArr[i] (3 == 3) 🌏Progress: [===========>--------------------------------------] 11/49 [Target: 1-array-B-5] - normal ✅ Successfully ran dslings\tests\array\array.5.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | intArr.begin() != intArr.end() [D2DS LOGI]: - ✅ | data == val (0 == 0) [D2DS LOGI]: - ✅ | data == val (1 == 1) [D2DS LOGI]: - ✅ | data == val (2 == 2) 🌏Progress: [============>-------------------------------------] 12/49 [Target: 1-array-B-6] - normal ✅ Successfully ran dslings\tests\array\array.6.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | intArr[0] == intArr[-4] (100 == 100) [D2DS LOGI]: - ✅ | intArr[1] == intArr[-3] (50 == 50) 🌏Progress: [=============>------------------------------------] 13/49 [Target: 2-vector-B-0-0] - normal ✅ Successfully ran dslings\tests\vector\vector.0.0.cpp 🎉 The code is compiling! 🎉 🌏Progress: [==============>-----------------------------------] 14/49 [Target: 2-vector-B-0-1] - normal ✅ Successfully ran dslings\tests\vector\vector.0.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: allocate: dslings\tests\vector\vector.0.1.cpp:24 - StackMemAllocator: try to allocate 4 bytes 🌏Progress: [===============>----------------------------------] 15/49 [Target: 2-vector-B-0-all] - normal ✅ Successfully ran dslings\tests\vector\vector.0.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: allocate: dslings\tests\vector\vector.0.cpp:19 - StackMemAllocator: try to allocate 10 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 24 bytes [D2DS LOGI]: - ✅ | d2ds::DefaultAllocator::allocate_counter() == 1 (1 == 1) 🌏Progress: [================>---------------------------------] 16/49 [Target: 2-vector-B-1-0] - normal ✅ Successfully ran dslings\tests\vector\vector.1.0.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 4 bytes [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001a86f578f80, bytes 4 🌏Progress: [=================>--------------------------------] 17/49 [Target: 2-vector-B-1-1] - normal ✅ Successfully ran dslings\tests\vector\vector.1.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: - ✅ | BigFiveTest::copy_constructor() [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001cc22a81790, bytes 20 [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001cc22a817f0, bytes 20 [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001cc22a819b0, bytes 20 [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001cc22a81770, bytes 20 🌏Progress: [==================>-------------------------------] 18/49 [Target: 2-vector-B-1-2] - normal ✅ Successfully ran dslings\tests\vector\vector.1.2.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 40 bytes [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001a914b20e50, bytes 20 [D2DS LOGI]: - ✅ | d2ds::DefaultAllocator::allocate_counter() == 2 (2 == 2) [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001a914b21070, bytes 40 [D2DS LOGI]: - ✅ | d2ds::DefaultAllocator::deallocate_counter() == 2 (2 == 2) [D2DS LOGI]: - ✅ | BigFiveTest::self_assignment() 🌏Progress: [===================>------------------------------] 19/49 [Target: 2-vector-B-1-all] - normal ✅ Successfully ran dslings\tests\vector\vector.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 4 bytes [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 00000179b2ff8fa0, bytes 4 [D2DS LOGI]: - ✅ | BigFiveTest::destructor() [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: - ✅ | BigFiveTest::copy_constructor() [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 00000179b2fe1040, bytes 20 [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 00000179b2fe11c0, bytes 20 [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 00000179b2fe13c0, bytes 20 [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 00000179b2fe13a0, bytes 20 [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 20 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 40 bytes [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 00000179b2fe1060, bytes 20 [D2DS LOGI]: - ✅ | d2ds::DefaultAllocator::allocate_counter() == 2 (2 == 2) [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 00000179b2fe0f30, bytes 40 [D2DS LOGI]: - ✅ | 2 == d2ds::DefaultAllocator::deallocate_counter() (2 == 2) [D2DS LOGI]: - ✅ | BigFiveTest::self_assignment() 🌏Progress: [====================>-----------------------------] 20/49 [Target: 2-vector-B-2] - normal ✅ Successfully ran dslings\tests\vector\vector.2.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | intArr1.empty() [D2DS LOGI]: - ✅ | intArr2.size() == 10 (10 == 10) 🌏Progress: [=====================>----------------------------] 21/49 [Target: 2-vector-B-3-0] - normal ✅ Successfully ran dslings\tests\vector\vector.3.0.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | intArr1.capacity() == intArr2.capacity() (0 == 0) [D2DS LOGI]: - ✅ | intArr1.size() == intArr2.capacity() (0 == 0) [D2DS LOGI]: - ✅ | intArr.capacity() == 10 (10 == 10) [D2DS LOGI]: - ✅ | intArr.capacity() == 4 (4 == 4) [D2DS LOGI]: - ✅ | intArr1.capacity() == intArr2.capacity() (4 == 4) [D2DS LOGI]: - ✅ | intArr3.capacity() == intArr2.capacity() (4 == 4) [D2DS LOGI]: - ✅ | intArr1.capacity() == 0 (0 == 0) [D2DS LOGI]: - ✅ | intArr2.capacity() == 10 (10 == 10) 🌏Progress: [======================>---------------------------] 22/49 [Target: 2-vector-B-3-all] - normal ✅ Successfully ran dslings\tests\vector\vector.3.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: - ✅ | intArr[0] == 1 (1 == 1) [D2DS LOGI]: - ✅ | intArr.capacity() == 2 (2 == 2) [D2DS LOGI]: - ✅ | intArr[1] == 2 (2 == 2) [D2DS LOGI]: - ✅ | intArr[2] == 3 (3 == 3) [D2DS LOGI]: - ✅ | intArr.size() == 3 (3 == 3) [D2DS LOGI]: - ✅ | intArr.capacity() == 4 (4 == 4) [D2DS LOGI]: - ✅ | intArr[1] == 2 (2 == 2) [D2DS LOGI]: - ✅ | intArr[0] == 1 (1 == 1) [D2DS LOGI]: - ✅ | intArr.capacity() == 8 (8 == 8) [D2DS LOGI]: - ✅ | intArr[intArr.size() - 1] == 2 (2 == 2) 🌏Progress: [=======================>--------------------------] 23/49 [Target: 2-vector-B-4] - normal ✅ Successfully ran dslings\tests\vector\vector.4.cpp 🎉 The code is compiling! 🎉 🌏Progress: [========================>-------------------------] 24/49 [Target: 2-vector-B-5] - normal ✅ Successfully ran dslings\tests\vector\vector.5.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- 🌏Progress: [=========================>------------------------] 25/49 [Target: 3-embedded-slist-B-0] - normal ✅ Successfully ran dslings\tests\embedded-list\embedded-slist.0.cpp 🎉 The code is compiling! 🎉 🌏Progress: [==========================>-----------------------] 26/49 [Target: 3-embedded-slist-B-1] - normal ✅ Successfully ran dslings\tests\embedded-list\embedded-slist.1.cpp 🎉 The code is compiling! 🎉 🌏Progress: [===========================>----------------------] 27/49 [Target: 3-embedded-slist-B-2] - normal ✅ Successfully ran dslings\tests\embedded-list\embedded-slist.2.cpp 🎉 The code is compiling! 🎉 🌏Progress: [============================>---------------------] 28/49 [Target: 3-embedded-slist-B-3] - normal ✅ Successfully ran dslings\tests\embedded-list\embedded-slist.3.cpp 🎉 The code is compiling! 🎉 🌏Progress: [=============================>--------------------] 29/49 [Target: 3-embedded-slist-B-4] - normal ✅ Successfully ran dslings\tests\embedded-list\embedded-slist.4.cpp 🎉 The code is compiling! 🎉 🌏Progress: [==============================>-------------------] 30/49 [Target: 4-slinked-list-B-0] - normal ✅ Successfully ran dslings\tests\slinked-list\slist.0.cpp 🎉 The code is compiling! 🎉 🌏Progress: [===============================>------------------] 31/49 [Target: 4-slinked-list-B-1] - normal ✅ Successfully ran dslings\tests\slinked-list\slist.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 16 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 16 bytes [D2DS LOGI]: allocate: dslings/common/common.hpp:64 - DefaultAllocator: try to allocate 16 bytes [D2DS LOGI]: - ✅ | d2ds::DefaultAllocator::allocate_counter() == 3 (3 == 3) [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001d6a91d1270, bytes 16 [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001d6a91d11b0, bytes 16 [D2DS LOGI]: deallocate: dslings/common/common.hpp:73 - DefaultAllocator: free addr 000001d6a91d14f0, bytes 16 [D2DS LOGI]: - ✅ | d2ds::DefaultAllocator::allocate_counter() == d2ds::DefaultAllocator::deallocate_counter() (3 == 3) AI-Tips-Config: https://d2learn.org/docs/xlings ---------E-Files--------- dslings\tests\slinked-list\slist.1.cpp ------------------------- Homepage: https://github.com/d2learn/xlings error: wait events in poller failed!删除main里的内容之后再次执行的输出
xmake xlings -D --project=. J:\cpp_project\d2ds d2x checker ✅ Successfully ran dslings\tests\dslings.0.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- 🌏Progress: [===>----------------------------------------------] 3/49 [Target: 0-dslings-B-1] - normal ✅ Successfully ran dslings\tests\dslings.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- 🌏Progress: [==============================>-------------------] 30/49 [Target: 4-slinked-list-B-1] - normal ✅ Successfully ran dslings\tests\slinked-list\slist.1.cpp 🎉 The code is compiling! 🎉 🌏Progress: [===============================>------------------] 31/49 [Target: 4-slinked-list-B-1] - normal ✅ Successfully ran dslings\tests\slinked-list\slist.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- AI-Tips-Config: https://d2learn.org/docs/xlings ---------E-Files--------- dslings\tests\slinked-list\slist.1.cpp ------------------------- Homepage: https://github.com/d2learn/xlings error: wait events in poller failed!删掉config.xlings里 checker 的 vscode,在终端中运行的输出依旧一致
🌏Progress: [===============================>------------------] 31/49 [Target: 4-slinked-list-B-1] - normal ✅ Successfully ran dslings\tests\slinked-list\slist.1.cpp 🎉 The code is compiling! 🎉 ---------C-Output--------- AI-Tips-Config: https://d2learn.org/docs/xlings ---------E-Files--------- dslings\tests\slinked-list\slist.1.cpp ------------------------- Homepage: https://github.com/d2learn/xlings error: wait events in poller failed!观察了一下xmake build,结束slist.1.cpp 要运行下一个的时候,cpu占用率迅速提升了,本来只有5-6这样,然后提升到了18,然后等待一段时间,大概十几秒二十秒,结束的时候内存占用从60mb突然提升到200多然后结束
-
dsx自动检测出现错误@SPeak 貌似还是在这个地方失败了

重新下载了文件后依旧只能执行到slist.1.cpp这一个,后续的无法检测,在我这边貌似是固定复现 -
dsx自动检测出现错误
退出前xmake build在高速运行 -
dsx自动检测出现错误xlings版本为0.0.4
-
dsx自动检测出现错误
slist.1.cpp练习完成后进入下一个检测,xmake会报错,然后退出检测,后续继续自动检测也同样报错
报错信息如下
Homepage: https://github.com/d2learn/xlings
error: wait events in poller failed!
error: execv(xmake xlings --project=. J:\cpp_project\d2ds d2x checker) failed(-1) -
json库的initializer_list创建问题@SPeak
按照这个思路,只保留一个列表初始化,用了一个辅助函数检测了每个元素是否是数组,如果是数组就检测长度是否是2,然后首元素是否是string,如果初始化列表里全部元素都符合就判定为object。这个代码运行成功了,感谢大佬Json(std::initializer_list<Json> init) { if (is_object_list(init)) { JsonObject dict; for (const auto &el : init) { auto pair = std::get_if<JsonArray>(&el.m_value); // 必须是长度为2的数组 assert(pair && pair->size() == 2); auto key = std::get_if<std::string>(&(*pair)[0].m_value); // 第一个元素必须是字符串 assert(key); dict.emplace(*key, (*pair)[1]); } m_value = std::move(dict); } else { m_value = JsonArray(init); } } bool is_object_list(std::initializer_list<Json> init) { return std::all_of(init.begin(), init.end(), [](const Json &el) { auto pair = std::get_if<JsonArray>(&el.m_value); if (!pair || pair->size() != 2) return false; return std::holds_alternative<std::string>((*pair)[0].m_value); }); } -
json库的initializer_list创建问题可能给每个类型都加一个构造函数也是有问题的设计,也许需要一个模板替代
-
json库的initializer_list创建问题用c++17 variant实现的json库,想实现用 {} 大括号快速构建一个嵌套的json对象,但是加入了initializerlist后出现了灾难性的构造问题,无法构造数组、对象类型,更无法构建复杂的嵌套类型。如果去掉其中一个列表初始化,只保留构建数组的初始化列表,也不能实现匹配的问题,除非在构造时手动标明类型,但这和列表初始化的初衷完全背离了
main
#include "include/json/json.h" #include <iostream> using myjson::Json; using std::cin, std::cout, std::endl; int main(int, char **) { // 数组也无法用initializer_list构造,除非将部分单参数构造变为隐式,但这本质就是错误的,可能导致一系列问题 Json json_1 { 3.141, 2, 17}; // Json json2 // { // {"pi", 3.141}, // {"happy", true}, // {"name", "Niels"}, // {"nothing", nullptr}, // { // "answer", { // {"everything", 42} // } // }, // {"list", {1, 0, 2}} // }; cout<<json_1.is_array()<<endl; cout<<json_2.is_object()<<endl; return 0; }json.h
#pragma once #include <cstddef> #include <initializer_list> #include <stdexcept> #include <string> #include <unordered_map> #include <utility> #include <variant> #include <vector> namespace myjson { class Json; template <typename... Ts> struct overload : Ts... { using Ts::operator()...; }; template <typename... Ts> overload(Ts...) -> overload<Ts...>; using JsonArray = std::vector<Json>; using JsonObject = std::unordered_map<std::string, Json>; class Json { public: using ValueType = std::variant<std::nullptr_t, bool, int, double, std::string, JsonArray, JsonObject>; public: Json() : m_value(nullptr) {} Json(const Json &) = default; Json(Json &&) = default; ~Json() = default; // 从基本类型构造 Json(std::nullptr_t) : m_value(nullptr) {} explicit Json(bool value) : m_value(value) {} // 防止 bool 构造为 int template<typename Int, typename = std::enable_if_t<std::is_integral_v<Int> && !std::is_same_v<Int, bool>>> explicit Json(int value) noexcept : m_value(static_cast<int>(value)) {} explicit Json(double value) : m_value(value) {} explicit Json(const std::string& value) : m_value(value) {} //const char* 是否需要构造存疑 // explicit Json(const char* value) : m_value(std::string(value)) {} explicit Json(JsonArray value) : m_value(std::move(value)) {} explicit Json(JsonObject value) : m_value(std::move(value)) {} Json(std::initializer_list<Json> init){ JsonArray array; for (const auto& element : init) { array.emplace_back(element); } m_value = std::move(array); } // object initializer_list构造 // Json(std::initializer_list<std::pair<const std::string, Json>> init) { // JsonObject dict; // for (const auto& p : init) { // dict.insert(p); // } // m_value = std::move(dict); // } Json &operator=(const Json &other) = default; Json &operator=(Json &&other) = default; public: [[nodiscard]] constexpr bool is_boolean() const noexcept { return std::holds_alternative<bool>(m_value); } [[nodiscard]] constexpr bool is_integer() const noexcept { return std::holds_alternative<int>(m_value); } [[nodiscard]] constexpr bool is_null() const noexcept { return std::holds_alternative<nullptr_t>(m_value); } [[nodiscard]] constexpr bool is_double() const noexcept { return std::holds_alternative<double>(m_value); } [[nodiscard]] constexpr bool is_string() const noexcept { return std::holds_alternative<std::string>(m_value); } [[nodiscard]] constexpr bool is_array() const noexcept { return std::holds_alternative<JsonArray>(m_value); } [[nodiscard]] constexpr bool is_object() const noexcept { return std::holds_alternative<JsonObject>(m_value); } template <typename T> const T &get() const { if (!std::holds_alternative<T>(m_value)) throw std::runtime_error("type mismatch"); return std::get<T>(m_value); } template <typename T> T &get() { if (!std::holds_alternative<T>(m_value)) throw std::runtime_error("type mismatch"); return std::get<T>(m_value); } JsonArray as_array() { if (!is_array()) throw std::runtime_error("Not an array"); return get<JsonArray>(); } JsonObject as_dict() { if (!is_object()) throw std::runtime_error("Not an dict"); return get<JsonObject>(); } private: ValueType m_value; }; } // namespace myjson