草庐IT

is_const

全部标签

c++ - 获取函数的地址并在编译时丢弃它 : is this valid C++?

我一直在寻找一种方法来将模板类型参数限制为那些实现给定签名功能的参数。我似乎已经找到了一个非常优雅的解决方案,它允许self记录代码和相当干净的、类似于概念的错误消息。唯一的问题是我不确定那是有效的C++还是恰好在clang和gcc中工作的东西。代码如下:#includeusingstd::enable_if;//let'ssaywewantsomethingwitha"regular"operator+classThing{public:Thingoperator+(constThing&){returnThing();}//thekindofoperatorwewant};clas

c++ - const 指针和 const 数组的输出

当我们有两个运算符用于输出对象和这些对象的数组,并尝试输出常量对象数组时,就会涉及到对象运算符。有没有办法强制数组的运算符与常量对象的c数组一起使用?示例代码:#include#includeusingstd::size_t;namespacenm{structC{inti;};templateusingc_c_array=C[N];inlinestd::ostream&operatorinlinestd::ostream&operatorconst&){returnlhs输出:1211此外,在我的例子中,2运算符使用1作为输出。 最佳答案

c++ - [conv]/6中语句 "The expression e is used as a glvalue if and only if the initialization uses it as a glvalue"的确切含义是什么

[conv]/6(重点是我的):Theeffectofanyimplicitconversionisthesameasperformingthecorrespondingdeclarationandinitializationandthenusingthetemporaryvariableastheresultoftheconversion.TheresultisanlvalueifTisanlvaluereferencetypeoranrvaluereferencetofunctiontype([dcl.ref]),anxvalueifTisanrvaluereferencetoob

C++符号分析: how to determine which static initialization is performed?

我想分析是什么原因导致我在Linux上由GCC(v.6.1.1)编译的共享C++库的大小。readelf-sWlibfoo.so告诉我特别大的函数叫做__static_initialization_and_destruction_0,例如:000000000026c42010272FUNCLOCALDEFAULT12__static_initialization_and_destruction_0(int,int)[clone.constprop.1774]我将-Wl,-Map,foo.map添加到CXX标志以生成链接器映射文件。在该映射文件中查找0x000000000026c420会

c++ - 返回填充在函数内部的 const char* vector 是否是明确定义的行为

我目前正在学习vulkan。在其中一个教程中,我看到了一个大致执行以下操作的函数:#defineSOMESTRING"HelloWorld!"std::vectorbuildVector(){std::vectorvec;vec.push_back(SOMESTRING);returnvec;}当我看到这个时,我想知道:这是定义的行为吗?字符串"HelloWorld!"的内容不是位于堆栈中,因此一旦函数返回就无效了吗?如果这是未定义的行为,那么正确的方法是什么?不幸的是,由于vulkanAPI,无法使用std::string。 最佳答案

c++ - 在 C++ 中将 char* 转换为 const char*

C++中如何将char*转换为constchar*?为什么程序1可以运行而程序2不能运行?程序1(工作):char*s="teststring";constchar*tmp=s;printMe(tmp);voidprintMe(constchar*&buf){printf("GivenStr=%s",buf);}程序2(不工作)char*s="teststring";printMe((constchar*)s);//typecastingnotworkingvoidprintMe(constchar*&buf){printf("GivenStr=%s",buf);}我收到的错误:x.c

c++ - 将 zlib 与 const 数据一起使用

要使用zlib压缩/解压缩数据,首先我需要设置一个名为z_stream的结构.z_stream有两个非常量指针叫做next_in和next_out.如果我想做这样的功能:voidungzip(std::vector&dst,conststd::vector&src){z_streamstrm;//morecode}和其他类似的voidgzip(std::vector&dst,conststd::vector&src);我该怎么办?在本地复制srcstd::vectorstd::vectortmp(src);并像这样将其用作源或设置指针,strm.next_in=const_cast(&

c++ - 由于类型不完整,在 static_assert 中使用 std::is_base_of 失败

我想做的是让一些类继承自extention类。问题是extention类必须知道它正在扩展哪个类。这可以像这样简单地实现:templateclassExtention{public:voidcheck()const{std::cout::value{};classBar:publicExtention{};Foo和Bar类显示了扩展的好坏用法。Foo().check();→Extentionisvalid:trueBar().check();→Extentionisvalid:false我想在编译时检查模板的有效性,这让我写了templateclassExtention{static_

c++ - _InterlockedCompareExchange 文档中 "The sign is ignored"的含义

_InterlockedCompareExchange的文档对每个参数说Thesignisignored.这是否意味着像0xffff和0x7fff(对于16位版本)这样的数字将被_InterlockedCompareExchange16等视为相等其他宽度内在函数?或者这是否意味着内在函数接受有符号和无符号整数?还是别的?如果这不是文档中的错误,它至少看起来是模棱两可的。 最佳答案 符号位不会被忽略,就像其他位一样进行比较。..CompareExchange..函数只关心位的相等性,不以任何特殊方式解释它们。在基于x86的系统上,它们

c++ - 为什么别名 int& 允许创建非函数类型 const (int&)?

通常在使用常量引用时会出现编译器错误,但在使用别名或使用模板时不会。为什么会这样?inta=5;usingmy_t=int&;my_tconstb=a;//#1OKint&constc=a;//#2Compilererror当运行最新的clang编译器(x86-64clang(实验性P1144))时,#1给我警告:[x86-64clang(experimentalP1144)#1]warning:'const'qualifieronreferencetype'my_t'(aka'int&')hasnoeffect[-Wignored-qualifiers]#2给出了错误:[x86-64