跳转至内容
  • 版块
  • 最新
  • 标签
  • 热门
  • Online Tools
  • 用户
  • 群组
折叠
品牌标识

D2Learn Forums

dustchensD

dustchens

@dustchens
关于
帖子
13
主题
2
群组
0
粉丝
0
关注
0

帖子

最新 最佳 有争议的

  • 项目想法: mcpp-start: 完全0基础的现代C++社区教程项目 - 想法讨论
    dustchensD dustchens

    感觉学习最难的地方就是内容太分散了,而且夹杂了很多和语言本身不相关的内容,可以说相关也可以说不相关,我的用词不一定准确。

    如果只限定C++20以上版本。完全用模块,且限定一种编译器,使用cmake,那入门的难度会降不少,但是中间还是有鸿沟,如果不能完全理解其中的历史包袱,还有大量使用,会非常难学。

    拿教培最成功的Java来说,初期学习很大程度是倾向于了解语言本身语法,还有一些实现特性,比如各种容器怎么用,特性背后是什么原理,IDEA一开,按钮点一点就可以了,并且出现问题也很容易定位错误,反正大伙都是随地大小抛异常的,叫什么名一清二楚。再深入一点,要学习各种轮子是怎么写的,ctrl按住鼠标点一点就跳进了源代码,源码非常清晰,顶多长了一点复杂了一点,抄也很容易抄明白。到了深入学习的时候,开始学习项目框架,多线程这些,再引入三方库,基本上很容易操作。最后的最后,哪怕学习不明白,了解的不够深入,也有spring这样的大杀器,它的原理很复杂,但是使用却很简单不同层级上@几下,很快就能搭建一个破烂项目,虽然很破烂,但是它跟高级项目是同一个东西,恭喜你,没有入门也半只脚踏进去了。

    但是C++不一样,一起跟它出现的是C,那就引出了无数的历史包袱,想要入门,门在哪里就成了一个问题,门在水面下,很多东西是以思想的形式存在的,并且一些新特性,本身就是散开的,都是为了解决之前分散在不同方面的问题,学了知道了但是不知道在哪串起来。
    第一个要学的就是内存管理的思想,为什么说是思想,而不是方法。我认为编程语言就是一步步抽象的过程,为什么要抽象,是因为要实现功能,很多写C的老保觉得底层更高级,以至于衍生出多汇编吹,不是这样的,计算机最终目的是实现人想要实现的东西,这个实现才是有意义的,编程语言实现的是语义,语义经过计算机执行得到结果,语义是我们书写最重要的东西。用什么汇编实现,底层到底怎么样,是次要矛盾!如果它是主要矛盾,那么大伙都应该在用 0 1实现语义才对,哪轮得到汇编,正因如此,表达语义更简单的汇编取代了打纸带写01,高级语言用更方便的语义表达让程序员能更好工作,AI也提供了用自然语言表达语义得到计算结果的能力。只不过我们这个领域大部分功能还是需要我们用编程语言实现语义,得到结果,或许几百年后编程可以用自然语言,直接通过更高级的编译器翻译成C++或者其他语言再变成机器码实现语义,得到程序。
    扯远了。。。。内存管理是一种思想,因此用虚拟机管理内存和手动管理内存,并没有本质的区别,只是实现不同,效果有略微差别罢了。C++处于一种半自动管理的状态,这个半自动是指相比于纯C,语言本身的机制自动帮你实现了部分管理内存的语义,RAII的思想就是这个半自动的衍生品。内存管理这部分要让新手能入门,就不能只讲构造析构,还有智能指针这些,而是要给出对应C语言在原始手动状态,是如何实现的,这样才能完全明白原理。
    就比如智能指针,它实现内存管理是借助了析构函数这个能在离开作用域自动调用的特性,相同的C语言代码,要借助一系列黑魔法才能实现同样的语义,给出这些代码有了对比就能更好讲解,让大伙知其所以然。
    再比如各种vector轮子的讲解,初学的时候一头雾水,为什么要用什么allocate和定位new,这时候把C实现new的代码贴出来,(先申请个空间,强转一下;对不同类型赋值,有的还需要再申请资源)这就又把new和delete是两件事的知识点再串起来了,然后又能引出这样拆分的好处:预先分配大块内存,定位new可以在指定位置用构造函数赋值。而主动调用析构,也就是通知房子空了,里面可能存在的资源也清理了。如果不拆分,那调用delete,就会把大块内存中间删掉一个内存,释放给操作系统,还要维护断开不连续的两个内存。如果项目能以一种思想的实现串连起分散的内容,或者按片划分,那对于学习真的很有帮助。

    然后就是各种历史遗留问题,enum、头文件本质是复制粘贴,其实就是那时候编译器不太智能硬件各种受限,导致后续一系列。。。。然后还要讲几个编译器,cmake,包管理,这些感觉都不是语言本身要学习的,而是历史遗留不得不学,讲解cmake这些东西,那就要对比着来,比如在vs里添加各种链接,才能体现有这么个配置文本的好处。扯不出来了,总之这些历史遗留和基础设施,弗如rust。后续建议扯不出来了


  • dsx自动检测出现错误
    dustchensD dustchens

    @SPeak 感谢大佬,已经解决了。不过我还是很疑惑,vector练习里也有自赋值的检测,为什么可以正常进入练习,也只是提示double free,为什么循环链表这里会出现死循环


  • dsx自动检测出现错误
    dustchensD dustchens

    @SPeak 大概定位到了问题,在我当前的代码下,slist.2.cpp里的 // d2ds::SLinkedList<int> intList2(intList1); 这些拷贝语句都会使得无法进入该练习
    https://github.com/dustchens/my-d2ds/tree/main/dslings/exercises/linked-list
    这是我出错的仓库链接


  • dsx自动检测出现错误
    dustchensD dustchens

    @SPeak 乱码确实没有了。
    是运行下一个检查没有成功,是否需要改slist.2.cpp里的内容?还是我把 .gitignore删掉上传全部内容?没有用过xmake,是不是我自己的代码出现了问题导致的这后面的运行不起来。。 我在修改slist.2.cpp里的内容,看看注释掉一部分后哪里有问题,定位到导致出错的语句,然后贴上自己SLinkedList.hpp里的代码,这样可以吗?


  • dsx自动检测出现错误
    dustchensD dustchens

    @SPeak

    删除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自动检测出现错误
    dustchensD dustchens

    @SPeak 貌似还是在这个地方失败了
    image.png
    重新下载了文件后依旧只能执行到slist.1.cpp这一个,后续的无法检测,在我这边貌似是固定复现


  • dsx自动检测出现错误
    dustchensD dustchens

    image.png
    退出前xmake build在高速运行


  • dsx自动检测出现错误
    dustchensD dustchens

    xlings版本为0.0.4


  • dsx自动检测出现错误
    dustchensD dustchens

    8d4188dd-70e0-4be6-ab0b-09b96d5833fc-a6ced52b-48ba-447c-9c74-40edf94c6e16.png

    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创建问题
    dustchensD dustchens

    @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创建问题
    dustchensD dustchens

    可能给每个类型都加一个构造函数也是有问题的设计,也许需要一个模板替代


  • json库的initializer_list创建问题
    dustchensD dustchens

    用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
    
  • 登录

  • 没有帐号? 注册

  • 登录或注册以进行搜索。
d2learn forums Powered by NodeBB
  • 第一个帖子
    最后一个帖子
0
  • 版块
  • 最新
  • 标签
  • 热门
  • Online Tools
  • 用户
  • 群组