草庐IT

is_const

全部标签

c++ - 使用模板减少 const 和非 const 非成员函数的代码重复

我有以下两个功能:Thing*find_thing_by_name(constString&name,Map&thing_map){autoit=thing_map.find(name);returnit->second;}constThing*find_thing_by_name(constString&name,constMap&thing_map){autoit=thing_map.find(name);returnit->second;}这只是我要解决的问题的一个简单示例。这些函数具有完全相同的主体,但我需要同时处理我的map的const和非const版本。我已经看到使用con

c++ - 使用 const 成员对自定义对象的 vector 进行排序

我想对包含const类对象的vector进行排序成员变量。不幸的是,我收到一条错误消息,指出“没有匹配函数来调用“swap””。当我删除const的关键字id,然后std::sort()适用于重载的operator和自定义比较功能。为什么会这样?我通常可以不对属于具有const成员变量的类的对象进行排序吗?#include#include#includeusingnamespacestd;structA{constintid;A(intid):id(id){}booloperatorvec;vec.emplace_back(3);vec.emplace_back(0);vec.empl

c++ - SFINAE std::isfinite 和使用 std::is_arithmetic 的类似函数

我在将一些代码从VS2013移植到GGC4.9和Clang3.5(使用libc++)时遇到了编译失败。代码的要点是#includestructFoo{operatordouble()const{return(101.0);}//Implicitconversiontodouble};intmain(int,char**){Foofoo;std::exp(foo);//Compilesstd::isfinite(foo);//Doesnotreturn(0);}我相信isfinite调用无法编译,因为cmath中的isfinite函数的返回类型声明为:typenamestd::enabl

java - Switch 语句 : Is the logic different in C v/s. Java 等其他语言?

我正在浏览thisC编程教程。它说:Theswitch-statementisactuallyentirelydifferent(fromotherlanguages)andisreallya"jumptable".Insteadofrandombooleanexpressions,youcanonlyputexpressionsthatresultinintegers,andtheseintegersareusedtocalculatejumpsfromthetopoftheswitchtothepartthatmatchesthatvalue.Here'ssomecodethatw

C++ 相关名称 : Is this typename required?

在a.hpp中我定义了:#includenamespaceBoard{templatestructGroupNode{usingPointType=std::pair;//...};}然后,在b.cpp中我定义了:#include"a.hpp"namespaceBoard{templatestructNodeList{usingStdList=std::list>;}}//andthenuseNodeListnl;上面的代码可以在没有任何警告的情况下在gcc-6和clang-3.9上编译。但是,Clion2016.3提示cannotresolvevariableGroupNodeinb

c++ - 错误 : argument of type char* is incompatible with parameter of type LPCWSTR

#include#includeusingnamespacestd;intmain(){char*file="d:/tester";WIN32_FIND_DATAFindFileData;HANDLEhFind;hFind=FindFirstFile(file,&FindFileData);//lineoferrorsaysargumentoftypechar*isincompatiblewithparameteroftypeLPCWSTR}我无法理解错误。错误是什么以及如何解决错误?我正在制作一个控制台应用程序,需要检查目录中是否有文件。 最佳答案

c++ - 获取 std::set 元素地址时从 ‘const int*’ 到 ‘int*’ 的无效转换

我收到以下错误error:invalidconversionfrom‘constint*’to‘int*’以下是我的程序#includeintmain(intargc,char**argv){std::setintSet;intSet.insert(1);intSet.insert(2);intSet.insert(3);intSet.insert(4);intSet.insert(5);int*pAddress=&(*(intSet.find(4)));}我想要std::set中元素的地址,此代码不会给Microsoft编译器带来任何编译错误,但g++会给出此编译错误。

c++ - 资格调整(const/volatile)可能导致歧义

谁能帮我理解为什么当getter是const时下面的代码无法编译(VS2010)?测试代码如下:#includeclasssocket{public://setter-throwexceptionversionvoidnon_blocking(boolmode){//...}//getter-errorcodeversionboolnon_blocking(boost::system::error_code&ec)const{//...}//setter-errorcodeversionvoidnon_blocking(boolmode,boost::system::error_cod

c++ - 如何识别代码中const char*和const char[]的类型?

constchar*s1="teststirg";constchars2[]="teststirg";我想要一个方法告诉我s1是“char*”而s2是“char[]”,怎么写这个方法? 最佳答案 使用模板:templateboolIsArray(T(&a)[SIZE]){returntrue;}templateboolIsArray(T*p){returnfalse;}这将在运行时进行评估。用法:if(IsArray(s1))...if(IsArray(s2))...如果有兴趣,您可以使用一些高级技术,它会告诉您这是编译时间。编辑:

c++ - static const string 成员变量是否总是在使用前初始化?

在C++中,如果我想定义一些non-localconststring,可以在不同的类、函数、文件中使用,我知道的方法是:使用定义指令,例如#defineSTR_VALUE"some_string_value"const类成员变量,例如classDemo{public:staticconststd::stringConstStrVal;};//thenincppstd::stringDemo::ConstStrVal="some_string_value";const类成员函数,例如classDemo{public:staticconststd::stringGetValue(){ret