草庐IT

copy_pointer

全部标签

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++ - std::copy 和 std::vector 问题

我明白为什么这会导致段错误:#include#includeusingnamespacestd;intmain(){vectorv;intiArr[5]={1,2,3,4,5};int*p=iArr;copy(p,p+5,v.begin());return0;}但为什么这不会导致段错误?#include#includeusingnamespacestd;intmain(){vectorv;intiArr[5]={1,2,3,4,5};int*p=iArr;v.reserve(1);copy(p,p+5,v.begin());return0;} 最佳答案

c++ - 使用 std::ifstream、std::istream_iterator 和 std::copy 不读取整个文件

我在一个188字节的文件中使用了以下代码:std::ifstreamis("filename",std::ios::binary);std::vectorbuffer;std::istream_iteratori_input(is);std::copy(i_input,std::istream_iterator(),std::back_inserter(buffer));std::cout但是它只读取了188个字节中的186个字节。我已经在十六进制编辑器和ls-al中确认了文件大小。 最佳答案 我不知道为什么,但默认情况下似乎会跳过

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++ - QFile::copy() 的进度条?

我正在制作一个在Qt中复制文件的程序。我想知道如何将QProgressBar与boolQFile::copy(constQString&fileName,constQString&newName)一起使用。这甚至可以通过copy函数实现吗?可以暂停复制过程吗? 最佳答案 你不能使用静态QFile::copy()方法来做到这一点。正如Maciej所说,您需要编写自己的类(class)。它应该使用两个QFile对象,一个用于读取,一个用于写入。分部分传输数据(例如整个文件大小的1%)并在每个部分后发出进度信号。您可以将此信号连接到进度对

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++ - 为什么 std::is_copy_constructible 的行为不如预期?

#includeintmain(){std::is_constructible_v;//false,asexpected.std::is_copy_constructible_v;//true,NOTasexpected!}根据cppref:IfTisanobjectorreferencetypeandthevariabledefinitionTobj(std::declval()...);iswell-formed,providesthememberconstantvalueequaltotrue.Inallothercases,valueisfalse.std::is_copy_c

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++ - STL 容器 move 语义并按值返回 : how many times of copying get avoided away?

我知道在C++11中,move语义已经在STL容器中实现以避免临时对象。人们说现在编写按值返回的函数是完美的。但我对究竟有多少次复制实际上被避免感到困惑。请看下面的例子:vectormyVector(){vectorres;res.push_back(4);res.push_back(5);returnres;}vectorv=myVector();我的理解是在c++03中,myVector返回res的拷贝(4,5复制了一次),在评估vectorv=myVector();时vector的复制构造函数vector(constvector&)被调用(4,5复制了两次)。但是在具有move语