草庐IT

const_object

全部标签

c++ - 重载解析和指向 const 的共享指针

我正在将大型代码转换为使用自定义共享指针而不是原始指针。我在重载解析方面有问题。考虑这个例子:#includestructA{};structB:publicA{};voidf(constA*){std::cout此代码正确写入“非常量版本”,因为qualificationconversions在隐式转换序列的排名中发挥作用。现在看一下使用shared_ptr的版本:#include#includestructA{};structB:publicA{};voidf(std::shared_ptr){std::cout){std::coutb;f(b);}此代码无法编译,因为函数调用不明

c++ - std::clamp - 检测函数返回值是否绑定(bind)到 const T&

最好不要将std::clamp返回值绑定(bind)到constref,如果它的min或max参数之一是右值.std::clamp的典型实现(非常简化):templateconstexprconstT&clamp(constT&value,constT&min,constT&max){returnvalue正如cppreferenceforstd::clamp中所述当有人写下时,情况很危险:intn=-1;constint&r=std::clamp(n,0,255);//risdangling有没有办法在编译时检测这些情况? 最佳答案

c++ - const 元素数组的类型

标准中是否规定常量元素数组与非常量元素数组的类型不同?这是我的代码和VC2010和GCC4.8.0的输出。谢谢。#include#include#includeintmain(){intarr_a[]={1,2};intconstarr_b[]={3,4};//orconstintarr_b[]={3,4};std::cout 最佳答案 C++11标准实际上说(3.9.3/1)(强调我的)[...]Thecv-qualifiedorcv-unqualifiedversionsofatypearedistincttypes;howev

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++ - 在实现 iterator 和 const_iterator 类时避免代码重复的最佳实践

关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭6年前。Improvethisquestion在实现诸如iterator和const_iterator或类似的类对时,避免代码重复的最佳实践是什么?人们是否通常使用大量const_casts根据const_iterator来实现迭代器?是否使用了某种特征类并最终将iterator和const_iterator定义为通用模板的不同实例?这似乎是一个足够普遍的问题,需要一个规范的解决方案,但我没有找到任何专门针对该问题的文章。

c++ - 我应该返回 gsl::span<const T> 而不是 const std::vector<T>&

我有一个带有std::vector成员的类和一个返回对该vector的const引用的成员函数。classdemo{public://...conststd::vector&test()const{returniv;}private:std::vectoriv;};我计划将成员类型更改为不同的数组,如具有足够功能和较小内存占用的容器类型(例如std::experimental::dynarray、std::unique_ptr)。因此,我认为最好不要将真正的容器作为const引用返回,而是将View作为gsl::span返回给元素。classdemo{public://...gsl::

c++ - 为什么 const void* 没有 std::free 重载?

我使用一个externC函数返回动态分配的constchar*.我想使用unique_ptr对其进行管理。但是没有std::free(constvoid*)重载所以我得到invalidconversionfrom'constvoid*'to'void*'并且必须使用const_cast().这只是标准库的缺陷还是背后有其他原因? 最佳答案 std::free继承自C标准库。C没有重载,因此无法继承const重载。虽然C++标准库已经用一些有用的重载扩展了继承的C库,但是free还没有添加const重载。要么从未考虑过这样的过载,要么

c++ - 避免使用 auto 关键字字面上重复 const 和非常量的代码?

好的,我做了一些研究,显然在这个主题上有很多重复的问题,仅举几例:Elegantsolutiontoduplicate,constandnon-const,getters?Howtoavoidoperator'sormethod'scodeduplicationforconstandnon-constobjects?HowdoIremovecodeduplicationbetweensimilarconstandnon-constmemberfunctions?等但我还是忍不住再次提出来,因为与c++14auto类型的返回值,我实际上是在复制函数体,唯一的区别是const函数限定符。c

c++ - 为什么这个来自Objective-C++的dynamic_cast调试成功但发布失败?

我在最新版本的Xcode(撰写本文时为9.4.1)中构建了一个C++框架,我再次在Xcode中从Objective-C++代码中使用它。我需要执行从一种指针类型到另一种指针类型的dynamic_cast。但是,dynamic_cast仅适用于调试版本,不适用于发布版本。关于dynamic_cast在Objective-C++中的工作方式,我是否缺少或理解导致此示例失败的某些内容?C++框架TestClass.hppclassParent{public://https://stackoverflow.com/a/8470002/3938401//musthaveatleast1virtu

c# - 如何将带有 const char* 的 C union 映射到 C# 结构?

在本地库的回调函数中,我需要访问一个espeak_EVENT数组。问题出在原C代码中的UNION语句:typedefstruct{espeak_EVENT_TYPEtype;unsignedintunique_identifier;//messageidentifier(or0forkeyorcharacter)inttext_position;//thenumberofcharactersfromthestartofthetextintlength;//wordlength,incharacters(forespeakEVENT_WORD)intaudio_position;//th