草庐IT

pointer_traits

全部标签

c++ - 标准转换 : Array-to-pointer conversion

这是来自ISO的要点:标准转换:数组到指针的转换:$4.2.1Anlvalueorrvalueoftype“arrayofNT”or“arrayofunknownboundofT”canbeconvertedtoanrvalueoftype“pointertoT.”Theresultisapointertothefirstelementofthearray.谁能解释一下,如果可能的话,用一个示例程序。我已经看过这些链接,但我无法理解:ArrayandRvalueIthinkImayhavecomeupwithanexampleofrvalueofarraytype

c++ - 从结构中调用 Pointer-to-Member-Function 指向的函数

我有一个具有特殊数据结构的类Test。Test类的成员是std::map,其中键是std::string,映射值是struct定义如下:typedefstruct{void(Test::*f)(void)const;}pmf_t;map初始化正常。问题是当我试图调用指向的函数时。我编了一个重现问题的玩具示例。在这里:#include#includeusingnamespacestd;classTest;typedefvoid(Test::*F)(void)const;typedefstruct{Ff;}pmf_t;classTest{public:Test(){pmf_tpmf={&T

C++ 问题 : pointers/memory addresses and subclasses

为什么允许我们运行这段代码:int*FunctionB(intx){inttemp=30;//morecodereturn&temp;}在我看来,我并没有像我说过的那样返回。为什么我把返回类型声明为指针就可以返回一个内存地址。指针不是指向内存地址的东西,实际上不是内存地址吗?classImage:publicBMP{public:voidinvertcolors();voidflipleft();voidadjustbrightness(intr,intg,intb);private:};在编译之前的代码时我得到这个错误:image.h:3:error:expectedclass-na

c++ - std::sort 在 std:vector of pointers 上失败

以下代码在对vector进行排序时崩溃。#include#include#includeusingnamespacestd;structFoo{intx;//inty;Foo():x(0){}};structCmp{booloperator()(Foo*p1,Foo*p2)const{if(p1->x!=p2->x)returnp1->xx;//if(p1->y!=p2->y)returnp1->yy;returntrue;}};intmain(){vectorv;for(inti=0;i为什么会这样? 最佳答案 boolopera

c++ - iterator_traits<InIter>::value_type 的创建是否会在传递时触发遵从? (异常测试)

我在故意为不特别遵守迭代器的函数抛出异常(出于测试目的)时遇到了麻烦。要了解我在做什么,请带上我的decorator_iterator结构:structdecorated_iterator:boost::iterator_adaptor,BaseIterator,boost::use_default,IteratorTag>{//....private:friendclassboost::iterator_core_access;/*usedtothrowanexceptionupondereference*/typenamebase_type::referencedereferenc

c++ - 使用 const 键类型引用调用 std::set of pointers 的 count 方法

我有一个类structS{boolfoo(constAType&v)const{returnvalues.count(&v);//compileerrorduetotheconstnessofv}private:std::setvalues;};这是一个简化版本。在实际代码中,foo做了一些复杂的事情。代码产生错误invalidconversionfrom‘constAType*’to‘std::set::key_type{akaAType*}’我认为foo应该采用'constAType&v'因为它不会改变v。成员变量“values”的类型不能为std::set,因为结构S的某些方法调

c++ - GCC 7,aligned_storage 和 "dereferencing type-punned pointer will break strict-aliasing rules"

我编写的代码在GCC4.9、GCC5和GCC6中没有警告。它在一些较旧的GCC7实验快照(例如7-20170409)中也没有警告。但在最近的快照(包括第一个RC)中,它开始产生关于别名的警告。代码基本上可以归结为:#includestd::aligned_storage::typestorage;intmain(){*reinterpret_cast(&storage)=42;}使用最新的GCC7RC编译:$g++-Wall-O2-cmain.cppmain.cpp:Infunction'intmain()':main.cpp:7:34:warning:dereferencingtyp

c++ - 模板化的 Pointer 类可以有一个虚拟析构函数吗?

在使用自制指针类实现pimpl惯用语时,我遇到了一个令人惊讶的启示(我知道:为什么要自己动手?但请耐心等待)。以下三个文件包含一个最小示例:指针.h:#pragmaoncetemplateclassPointer{public:Pointer(T*p=0):_p(p){}virtual~Pointer(){delete_p;}private:voidoperator=(constPointer&);Pointer(constPointer&);private:T*_p;};Foo.h:#pragmaonce#include"Pointer.h"structFoo{Foo();~Foo(

c++ - Clang 提示 : "pointer is initialized by a temporary array"

我有一个不同长度的(指向)数组的数组,我了解到我可以使用复合文字来定义它:constuint8_t*constminutes[]={(constuint8_t[]){END},(constuint8_t[]){1,2,3,4,5END},(constuint8_t[]){8,9,END},(constuint8_t[]){10,11,12,END},...};gcc很好地接受了这一点,但clang说:指针由一个临时数组初始化,它将在完整表达式结束时被销毁。这是什么意思?代码似乎可以正常工作,但话又说回来,许多事情似乎在指向不再分配的内存时可以正常工作。这是我需要担心的事情吗?(最终我真

c++ - std::vector of function pointers:不同的模板参数

为什么下面会编译std::vectorfunc_ptrs;但这不是std::vectorfunc_ptrs?在第二种情况下,我收到了那些丑陋的STL错误消息之一,所以我不打算将所有内容都放在这里,但在消息的末尾我得到了这个/usr/include/c++/4.8/bits/stl_construct.h:102:30:error:ISOC++forbidsincrementingapointeroftype‘int(*)(double)’[-fpermissive]for(;__first!=__last;++__first)这似乎暗示C++将类型int(double)转换为int(*