草庐IT

Markdown_use

全部标签

c++ - 有没有权威的方法来防范c++中的 "use after move"错误?

我发现开发人员很容易犯这些错误。是否有避免这种情况的最佳实践或权威方法?是否有适用于多个平台的编译器标志或提示? 最佳答案 一个有效的经验法则:永远不要使用std::move或std::forward并且永远不要类型转换为右值(或通用)引用。如果您从不从变量或引用移动,那么您就不会犯在之后使用它的错误。这种方法显然有一个缺点,因为这些实用程序在某些情况下可用于在移动足够时将拷贝转换为移动;更不用说必要的情况了。适用于您自己的类型的方法:将断言添加到成员函数中,以验证实例是否已从中移出,并依赖它们在测试期间触发。“移动”状态将需要存储

keil 报错 *** Target ‘Target 1‘ uses ARM-Compiler ‘Default Compiler Version 5‘ which is not available

问题:***Target‘Target1’usesARM-Compiler‘DefaultCompilerVersion5’whichisnotavailable.这个错误是由于使用的ARM编译器“DefaultCompilerVersion5”不可用导致。原因是新版的keil不在自动下载v5版本的编译器,但是老版本使用的v5,所以需要手动安装v5的编译器。下载v5.06的编译器并添加到keil,下载链接如下:链接:https://pan.baidu.com/s/1HKY34HP4zjkDPGd1ikbX4w?pwd=gych提取码:gych具体操作方法:(参考的是dxh_wds的资料)1.进

c++ - 类方法声明中的 decltype : error when used before "referenced" member is declared

考虑followingcode:structtest{autofunc()->decltype(data){}//ERRORintdata;};intmain(){testt;t.func();}它给出了以下错误:main.cpp:2:29:error:'data'wasnotdeclaredinthisscopeautofunc()->decltype(data){}但是,如果我将data放在func()之上,它不会给出任何错误(livecode):structtest{intdata;autofunc()->decltype(data){}};...所以我的问题是,为什么declt

c++ - -Wconversion warning while using operator <<= on unsigned char

当我用gcc编译以下代码时:intmain(){unsignedcharc=1;c我收到这个警告:conversionto‘unsignedchar’from‘int’mayalteritsvalue[-Wconversion]为什么?这段代码有什么问题?其实,我真的可以使用oprator吗?在unsignedchar上变量?编译命令:g++test.cpp-Wconversion-otest.exe 最佳答案 这是一个有效的警告:c相当于:c=c和的规则假设操作数被提升,在这种情况下将提升为int结果是提升的类型。所以会有一个从i

c++ - Noexcept 对派生类构造函数的 promise : can that be used without promising noexcept on base constructor?

假设我有一个类classC:publicB{public:C()noexcept;}noexcept说明符是否需要基类的相同promise?也就是说,当我考虑使用noexcept时,我是只看C::C()的行为还是我还需要考虑B::B()是否可能抛出异常?例如,如果B::B抛出异常,它会传播到C::C还是传播到请求新类实例的代码?--如果传播到C::C,如果基类不是noexceptforconstructor,那将是避免noexceptforconstructor的原因之一。 最佳答案 技术上†不要求将基类构造函数声明为noexcep

c++ - `constexpr` 变量 "used in its own initializer": Clang vs. GCC

这个问题似乎与anexistingone有关,但我不明白theanswerthere中提供的“可移植解决方法”(涉及constautothis_=this;)而且我认为下面的例子更容易理解。我正在使用以下C++17代码片段(livedemo):#includestructTest{constchar*name_{nullptr};constTest*src_{nullptr};constexprTest(constchar*name)noexcept:name_{name}{}constexprTest(constTest&src)noexcept:src_{&src}{name_=s

c++ - "uses of target_link_libraries must be either all-keyword or all-plain"

我设法构建了llvm和clang,现在我正在尝试根据clangdocs创建一个ClangTool.但是当我尝试构建它时出现以下错误:CMakeErrorattools/clang/tools/loop-convert/CMakeLists.txt:6(target_link_libraries):Thekeywordsignaturefortarget_link_librarieshasalreadybeenusedwiththetarget"loop-convert".Allusesoftarget_link_librarieswithatargetmustbeeitherall-k

c++ - 错误 : use of deleted function ‘std::thread::thread(const std::thread&)'

下面的代码编译并按预期工作。结构(类)A派生自std::thread并扩展了一个int。main代码创建一些线程,然后等待它们完成。问题在于,虽然代码编译时没有结构A中的析构函数,但当析构函数未注释时(~A(){})我得到:error:useofdeletedfunction‘std::thread::thread(conststd::thread&)'我不知道为什么。此外,我不明白为什么代码既适用于push_back也适用于emplace_back而根据我的理解它不应该适用于push_back.#include#include#includestructA:std::thread{i

将多个R脚本渲染为单个Markdown文件中的Markdown

我有几个R脚本已记录在使用#'目的是将所有脚本组合到一个.Rmd文件。我从这个帖子组合多个真的很直接.Rmd使用主体中的代码块的文件.Rmd文件这很好,但我更喜欢将代码保留为.R由于文件为其预期目的而运行得更快,并且文档的渲染不会经常发生。首先,我在主要的降价文件中尝试了这一点:```{r,child="script.R"}```但这并没有正确地渲染-与#'S存在。然后我试图使用此中描述的内容博客文章为了将R脚本组合在单个Markdown文件中:```{r}library(rmarkdown)rmarkdown::render("script.R")```但这只是产生script.md并且不会

c++ - 为什么 using 指令不是 "associate"与普通函数?

根据thisquestion在using指令之后定义类方法是有效的,而不是将它们包含在namespaceblock中。然而,对于普通函数来说,情况似乎并非如此。考虑:问候语.hh#pragmaoncenamespaceNS{classGreeting{public:voidhello();};voidotherHello();}问候语.cc#include"Greeting.hh"#includeusingnamespaceNS;voidGreeting::hello(){std::coutmain.cc#include"Greeting.hh"intmain(){NS::Greeti