草庐IT

pointer_traits

全部标签

c++ - 使用 "pointer to volatile"是否始终阻止编译器优化?

这里是问题所在:yourprogramtemporarilyusessomesensitivedataandwantstoeraseitwhenit'snolongerneeded.使用std::fill()本身并不总是有帮助-编译器可能会决定以后不会访问内存块,因此删除它是浪费时间并消除删除代码。Userybungalobill建议usingvolatilekeyword:{charbuffer[size];//obtainandusepasswordstd::fill_n((volatilechar*)buffer,size,0);}目的是在看到volatile关键字时,编译器将不

通过单个函数使用2Pointers的总和和两个数字的差异

主函数应具有函数调用,例如adddiff(&p,&q)...函数定义就像voidadddiff(*a,*b),它应该同时执行加法和减法,它不应返回值,也不应不应在AddDiff方法中使用打印语句。看答案这两个参数均通过引用传递,因此您可以使用它们将结果“返回”到主函数。但是,请记住要在本地保存其价值,然后再覆盖它们!voidadddiff(int*a,int*b){intorig_a=*a;intorig_b=*b;*a=orig_a+orig_b;*b=orig_a-orig_b;}

c++ - std::string 的 type_traits 段错误

从UsingSFINAEtocheckforglobaloperator收集信息和templates,decltypeandnon-classtypes,我得到了以下代码:http://ideone.com/sEQc87基本上,我将两个问题的代码结合起来,如果它有ostream声明,则调用print函数,否则调用to_string方法。摘自问题1namespacehas_insertion_operator_impl{typedefcharno;typedefcharyes[2];structany_t{templateany_t(Tconst&);};nooperatorstruct

c++ - 错误 C4703 : potentially uninitialized local pointer variable 'pNamesPtr' used

我正在做一个加密项目,在尝试编译程序时遇到了以下错误。main.cpp(520):errorC4703:potentiallyuninitializedlocalpointervariable'pNamesPtr'used==========Build:0succeeded,1failed,0up-to-date,0skipped==========DLLNAMES[i].UsedAlready=0;}*dwOutSize=(DWORD)pNamesPtr-(DWORD)pBuffer;//*有人可以帮我解决这个错误吗?您是否需要更多代码才能得到好的答案?

c++ - 哪个 Clang 警告等同于 GCC 的 Wzero-as-null-pointer-constant?

我们的项目使用C++11/14,我们希望使用nullptr而不是0或NULL指针,即使0(作为整数文字)是允许的。我有以下代码:intmain(){int*ptr1=nullptr;//#1int*ptr2=0;//#2}如果我使用GCC(5.3.0)和标记-Wzero-as-null-pointer-constant进行编译,它会在#2中发出警告,但我可以'在Clang中找不到类似的标志。如果我使用Clang(3.7.1)和标志-Weverything编译代码,我不会收到任何关于#2的警告。那么,有什么办法可以在Clang中得到类似的警告吗? 最佳答案

c++ - "The World' s Dumbest Smart Pointer 有什么意义?”

这个问题在这里已经有了答案:Useofobserver_ptr(9个回答)关闭6年前。提案N4282提倡一种称为observer_ptr的新型非拥有智能指针。它的工作名称是exempt_ptr,旨在替代“原始指针”。但我不明白它的目的,尤其是对于它旨在实现的目标的这个假设代码:structdo_nothing{templatevoidoperator()(T*){};//donothing};templateusingnon_owning_ptr=unique_ptr;即使看了文章,我也不明白什么都不做的智能指针的用途。与非拥有的shared_ptr或原始指针相比,它有什么优势?

c++ - Scala 和 C++ traits 之间的关系是什么

Traits是Scala和C++中使用的一个概念(尽管在C++中它更像是一个习语,而不是一个集成到语言中的概念)。不过,这些概念之间的关系对我来说并不明显。Scala和C++traits之间有什么关系? 最佳答案 他们根本没有关系。在C++中,traits类是一个辅助对象,它告诉您有关类型的一些信息,这些信息您无法从类型名称本身获得。C++traits实际上更类似于Scala的deffoo[A:Manifest]表示法(一个我不知道正确名称的功能。)Scala的特性实际上更像C++的多重继承(尽管它们在细节上有所不同)。我实际上相当

c++ - 练习 : pointers and references in C++

这是正确的吗:a)指向一个字符的指针p1:char*p1;b)指向char的常量指针p2:char*constp2;c)指向常量char的指针p3:constchar*p3;d)指向常量char的常量指针p4:constchar*constp4;e)对char的引用r1:char&r1;f)对常量char的引用r2:constchar&r2;如果有任何错误,请告知我好吗? 最佳答案 他们都是对的。我看不到任何错误:-) 关于c++-练习:pointersandreferencesinC+

c++ - 为什么隐含的 "lambda to function pointer conversion"禁止静态成员的 "by reference"捕获?

C++11标准说(或者至少,我拥有的版本——不是最终版本):Theclosuretypeforalambda-expressionwithnolambda-capturehasapublicnon-virtualnon-explicitconstconversionfunctiontopointertofunctionhavingthesameparameterandreturntypesastheclosuretype’sfunctioncalloperator.我理解为什么无法从有状态lambda中获取函数指针,因为函数指针本身不能保存任何数据。但是当捕获的对象只是一个静态成员/静

如何正确地从函数(type_traits)提供向量

我如何返回std::vector由此operator+()?#include#include#include#include#includetemplatestructis_std_vector:std::false_type{};templatestructis_std_vector>:std::true_type{};templatestd::enable_if_t>::value>operator+(T&&vec1,T&&vec2){for(inti=0;i(vec2.at(i)));returnvec1;}intmain(){std::vectorvec1{1,2,3,4,5};std