草庐IT

is_const

全部标签

c++ - 警告 C4172:返回对绑定(bind)到局部变量的 const std::string 的引用。它有多安全?

我刚刚在工作中构建我们的一个项目,我看到添加了一个新功能:conststd::string&ClassName::MethodName()const{return"";}编译器给出警告:WarningC4172:returningaddressoflocalvariableortemporary我认为编译器是对的。这个函数的安全性如何?请注意,该函数不会返回constchar*,这没有问题,因为字符串文字具有静态存储持续时间。它返回对conststd::string的引用 最佳答案 是的,它不安全。返回局部变量或临时变量的地址并取消

c++ - const char * 和 char *

我明白了char*s="HelloWorld!";存储在只读内存中,不能通过指针修改字符串文字。这和有什么不同constchar*s="HelloWorld!";另外'string'的类型是char*还是constchar*? 最佳答案 区别在于后者合法,前者不合法。这是在C++11中所做的更改。形式上,"HelloWorld!"的类型为constchar[13];它可以转换为constchar*。在过去,它的类型可能是char[13],可以转换为char*。C++通过添加const更改了数组的类型,但保留了对char*的转换,以便

c++ - 编译器如何在 C++ 中获取 const 的地址?

正如此处讨论的那样WhichmemoryareaisaconstobjectininC++?,编译器在编译代码时可能不会为常量分配任何存储,它们可能会直接嵌入到机器代码中。那么编译器如何获取常量的地址呢?C++代码:voidf(){constinta=99;constint*p=&a;printf("constant'svalue:%d\n",*p);} 最佳答案 一个常量是否会被分配任何存储空间完全取决于编译器。允许编译器根据As-If执行优化规则,只要程序的可观察行为没有改变,编译器可能会为a分配存储空间,也可能不会。请注意,标

C++ 通过 const 引用传递 const 指针

嗯,在VC2012中很奇怪,我似乎无法弄清楚通过const引用将const指针传递到模板参数是非const指针的模板类函数的语法,即:templatestructFoo{voidAdd(constT&Bar){printf(Bar);}};voidmain(){Foofoo;constchar*name="FooBar";foo.Add(name);//Causeserror}所以我在这里简化了我的问题,但基本上我希望“Add”的参数有一个constTieconstchar*。我试过:voidAdd(const(constT)&Bar);typedefconstTConstT;void

c++ - const 函数重载

这个问题在这里已经有了答案:FunctionswithconstargumentsandOverloading(3个答案)关闭9年前。我很困惑为什么下面的代码没有产生任何错误,因为传递给显示的参数是相同的类型,即char。const真的有区别吗?#includeusingnamespacestd;voiddisplay(char*p){cout编辑根据答案,永远不会调用第一个显示,这是正确的,输出也是如此。但假设我这样做:intmain(){char*p="Hello";display(p);//nowfirstdisplayiscalled.display("World");}编译器

c++ - "most important const"与 auto_ptr : Why the code does not compile?

以下代码无法在VisualC++2008或2010上编译:#includestructA{};std::auto_ptrfoo(){returnstd::auto_ptr(newA);}conststd::auto_ptrbar(){returnstd::auto_ptr(newA);}intmain(){conststd::auto_ptr&a=foo();//mostimportantconstconststd::auto_ptr&b=bar();//errorC2558://class'std::auto_ptr'://nocopyconstructoravailableorco

c++ - C & C++ : What is the difference between pointer-to and address-of array?

C++11代码:inta[3];autob=a;//bisoftypeint*autoc=&a;//cisoftypeint(*)[1]C代码:inta[3];int*b=a;int(*c)[3]=&a;b和c的值相同。b和c有什么区别?为什么它们不是同一类型?更新:我将数组大小从1更改为3。 最佳答案 sizeof运算符的行为应该有所不同,其中之一,尤其是当您将a的声明更改为不同数量的整数时,例如inta[7]:intmain(){inta[7];autob=a;autoc=&a;std::cout对我来说,这会打印:428那是因

c++ - 错误 MSB8008 : Specified platform toolset (v120) is not installed or invalid

这个问题在这里已经有了答案:WhatdoIneedtoinstallforC++project/VS2013onTeamCityserverorTeamFoundationBuildService?(1个回答)关闭8年前。我已经下载了一个C++项目。我打开了.sln文件并尝试构建项目。我收到以下错误:错误MSB8008:指定的平台工具集(v120)未安装或无效。请确保选择了受支持的PlatformToolset值。我已经检查过平台工具集是否设置为v120。这里有什么问题?

c++ - 将 const 和 decltype 与指针变量一起使用

下面的代码允许我更改*p2的值,即使p2是用const声明的。int*p1;constdecltype(p1)p2=newint(100);*p2=200;cout但是,如果我使用“int*”而不是“decltype(p1)”,那么编译器会标记错误。constint*p2=newint(100);*p2=200;cout我正在使用g++(Ubuntu4.8.2-19ubuntu1)4.8.2。应用于指针变量时,decltype是否忽略const说明符? 最佳答案 constdecltype(p1)p2表示int*constp2。这意

c++ - 为什么 std::is_function 对简单函数和 lambda 返回 false?

有如下一段代码:#include#includetemplate::value>::type>intfun(Ff)//line8{returnf(3);}intl7(intx){returnx%7;}intmain(){autol=[](intx)->int{returnx%7;};fun(l);//line23//fun(l7);thiswillalsofaileventhoughl7isaregularfunctionstd::cout::value;//prints1}我会得到以下错误:main2.cpp:Infunction‘intmain()’:main2.cpp:23:8: