草庐IT

C++ is_member_pointer 实现

在c++标准库中,is_member_pointer实现为templatestruct__is_member_pointer_helper:publicfalse_type{};templatestruct__is_member_pointer_helper:publictrue_type{};///is_member_pointertemplatestructis_member_pointer:public__is_member_pointer_helper::type>::type{};有人可以解释一下_Cp是如何推导出来的吗?它像魔术一样工作。 最佳答

c++ - C 和 C++ : Array element access pointer vs int

如果您执行myarray[i]或将myarray[i]的地址存储在指针中,是否存在性能差异?编辑:这些指针都是在我的程序中一个不重要的步骤中计算出来的,性能不是标准。在关键部分,指针保持静态并且不被修改。现在的问题是这些静态指针是否比一直使用myarray[i]更快。​​ 最佳答案 对于这段代码:intmain(){inta[100],b[100];int*p=b;for(unsignedinti=0;i在g++中使用-O3优化构建时,语句:a[i]=i;产生汇编输出:mov%eax,(%ecx,%eax,4)和这个声明:*p++=

c++ - 在 C++ 中递增常量

有人可以向我解释为什么这段代码有效吗?我觉得编译器不应该允许我做我所做的事情(移动一个int指针指向一个constint),或者我至少会期待编译器警告或段错误。改变常量值的想法似乎是错误的。代码:#includeusingnamespacestd;structtest_struct{inti;constintj;};intmain(){cout输出:Createastructwithinti=100andconstintj=101.100101Createpointerpandpointittointi.100Incrementpointerp,whichshouldnowbepoin

c++ - "' void* ' is not a pointer-to-object type"在没有 void* 的代码中?

我的代码有问题。在Xcode或使用C++11编译器中,此代码运行良好。但是,当我将此代码提交给在线法官时,判决显示“编译错误”。我认为他们使用的是C++4.7.1编译器,当我尝试编译它(使用Ideone)时,它说:prog.cpp:Infunction'voidprintArray(int)':prog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-objecttypeprog.cpp:27:error:'void*'isnotapointer-to-ob

c++ - 指向数组的指针

我想知道,你能在一个数组中创建一个指向一组变量的指针吗?像这样array[20]{'a','b','c',...}pointer=array[6through10];那么你可以说...*pointer[0]==array[6];和*pointer[5]==array[10];和*指针的长度5==sizeof(*pointer)\sizeof(type);确定让我解释一下我想要完成的事情,也许这会让我清醒一点。我想将整个文件读入缓冲区,但我想逐个读取。将一个小数组传递给read()然后将其循环到更大的数组中达不到目的。我希望我可以直接“指向”我想要填充的缓冲区中的一个区域,并将其传递给r

c++ - dladdr : pointer-to-function vs pointer-to-object

希望这是一个相当简单的C++问题(而不是语言律师问题)。如何在C++中使用GNU扩展dladdr?通常人们会用C编写以下内容:#ifndef_GNU_SOURCE#define_GNU_SOURCE#endif#includestaticvoidwhere_am_i(){}intmain(){Dl_infoinfo;dladdr((void*)&where_am_i,&info);return0;}但是使用clang可以看到转换可能无效:$clang--versionDebianclangversion3.6.2-3(tags/RELEASE_362/final)(basedonLLV

c++ - 未指定的指针转换在 C++14 中的行为如何?

某些指针转换的结果被描述为未指定。例如,[expr.static.cast]/13:Aprvalueoftype“pointertocv1void”canbeconvertedtoaprvalueoftype“pointertocv2T,”[...]IftheoriginalpointervaluerepresentstheaddressAofabyteinmemoryandAsatisfiesthealignmentrequirementofT,thentheresultingpointervaluerepresentsthesameaddressastheoriginalpoint

c++ - std::move into static_pointer_cast:为什么 static_pointer_cast 没有右值引用重载?

假设我们有一个函数需要一个按值共享的指针。(在现实生活中的例子中,我通过右值引用获取它并将其转发给成员。)voidf(std::shared_ptrptr){...}但是我们只有一个指向基类的共享指针,所以我们使用static_pointer_cast:std::shared_ptrptr=std::make_shared();f(std::static_pointer_cast(ptr));第一个赋值(从临时构造ptr)是否触发了引用计数的原子递增和递减,或者共享指针是否被移动?(请注意,它正在向上转换。)在static_pointer_cast中有引用计数的原子增量。如果我们不再需

c++ - 2 个重载具有相似的转换 - 内置运算符 integer[pointer-to-object]

我有以下类(class):classDictionaryRef{public:operatorbool()const;std::stringconst&operator[](std::stringconst&name)const;//...};然后我尝试使用它:DictionaryRefref=...;ref["asdf"];//error输出提示两个重载,但只列出一个:1>...:errorC2666:'DictionaryRef::operator[]':2overloadshavesimilarconversions1>...:couldbe'conststd::string&D

C++ : Understanding "this" Pointer

这个问题在这里已经有了答案:InC++,whyistheaddresschangedwhenthepointerisconverted?(3个答案)关闭6年前。我想了解“this”指针。我认为“this”指针指的是类对象的值。但是,在下面的代码中,我可以看到“this”指针的不同值:#includeclassInterfaceA{public:virtualvoidfuna()=0;};classInterfaceB{public:virtualvoidfunb()=0;};voidglobala(InterfaceA*obj){printf("globalA:pointer:%p\n