草庐IT

const-char

全部标签

c++ - 如何找到应该是 const 的 C++ 函数?

我有这个代码:#includeclassA{public:intdoit(){return5;}intdoit2()const{i++;returni;}inti;};intmain(){Aa;printf("%d\n",a.doit());return0;}使用g++-Wall-Wpedanticmain.cpp可以干净地编译。有没有办法让g++说“A::doit()应该标记为const”?g++4.8有-Wsuggest-attribute=const但在这种情况下它似乎不起作用。g++-Wall-Wpedantic-Wsuggest-attribute=constconst_ma

c++ - 如何干净地使用:const char* 和 std::string?

tl:drHowcanIconcatenateconstchar*withstd::string,neatlyandelegantly,withoutmultiplefunctioncalls.Ideallyinonefunctioncallandhavetheoutputbeaconstchar*.Isthisimpossible,whatisanoptimumsolution?初始问题到目前为止,我在C++中遇到的最大障碍是它如何处理字符串。在我看来,在所有广泛使用的语言中,它处理字符串的能力最差。我见过其他与此类似的问题,这些问题的答案要么是“使用std::string”,要么只

c++ - Typedef、模板和 const 关键字

我在使用带有const关键字(用于函数参数类型)的模板时遇到问题,为了说明这一点,我创建了一个小代码:templatestructMethodCallerFactory{typedefReturnType(*Type)(ClassType*,Args...);templatestructMethod{staticTypecreateMethodCaller(){ReturnType(*caller)(ClassType*,Args...)=[](ClassType*obj,Args...args)->ReturnType{ReturnType(ClassType::*ptr)(Args

c++ - 这是 `` const_cast`` 的有效用法吗?

C++11标准更改了erase()的签名标准容器的方法:他们现在接受const_iterators而不是iterator秒。本文档解释了基本原理:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2350.pdf现在,如果一个人执行std::vector直接用constT*就可以了和T*分别作为常量和可变迭代器类型。所以在erase()方法我们可能有这样的代码:iteratorerase(const_iteratorit){...for(;it!=end()-1;++it){//Destroythecurrenteleme

c++ - 将 unique_ptr 的 vector 传递给函数,const 引用

我通常会像这样传递一个包含原始指针的vector:someFunc(conststd::vector&classList){..}我想知道你是否可以像这样对unique_ptr做同样的事情:someFunc(conststd::vector>&classList){..}?它的意思是一样的吗?即只读。 最佳答案 是的,你可以。不,这不是同一件事:std::unique_ptr表示指针拥有的资源。通用原始指针可以具有许多其他语义。关于const的正确性,使用迭代器或operator[]访问vector的元素将产生对std::uniqu

指针函数/const 指针函数的 C++ 模板部分特化没有区别吗?

让我们考虑以下代码:templateclassFoo{};templateclassFoo{};templateclassFoo{};当我尝试编译它时(ideone)它没有告诉我这两个模板特化是相同的。这是令人惊讶的,因为通常U*和U*const是不同的东西(第二个是const指针)。这里有什么问题吗? 最佳答案 在确定函数的类型(通俗地称为其签名)时,将删除顶级cv限定符。§8.3.5/5...Thetypeofafunctionisdeterminedusingthefollowingrules....Afterproducin

C++ 从 char* 到 char 的无效转换 (char* = *string.begin() )

我有以下代码:std::stringextract(){fstreamopenfile("/home/name/Documents/testfile");std::stringteststring;longlocation=4;longlength=2;teststring.resize(length);char*begin=*teststring.begin();openfile.seekp(location);openfile.read(begin,length);returnteststring;}此代码应该返回在文件中找到的字符串。例如,如果文件的内容是StackOverflo

c++ - printf int 到带有左填充空格的 char 数组

我有一点脑死亡的时刻。我必须将int的字符串表示形式存储到char[]中,但ascii表示形式必须用空格填充。snprintf将完成这项工作。chardata[6];intmsg_len=10;std::snprintf(data,6,"%*d",5,msg_len);//"10"我只是想知道是否有更优雅的方法来做到这一点。我可以访问C++11还有一点问题,我认为snprintf也会添加一个终止符,我必须避免这种情况。我可以有一个中间缓冲区并将其复制到我的数据中,但这会增加额外的复杂性。我需要就地进行,因为这些数据结构是我必须发送到接受这种格式输入的服务器的消息的一部分。消息看起来像:

c++ - const 参数值为 0 的重载决策不正确

我有一个类B,它有两个重载函数intSet(B*);和intSet(constA&);。A类需要一个构造函数参数unsignedchar。当使用值为0的constunsignedchar调用Set时,它被解析为Set(B*)而当传递的值不为零时,它解析为Set(constA&)(按照我的预期)。重载解析在非constunsignedchar上正常工作,但在值设置为0的constunsignedchar上失败。为什么?以下代码说明了使用const和非constunsignedchar调用Set时的差异#includeusingnamespacestd;classA{charm_byteV

c++ - 将 std::shared_ptr<char> 转换为 std::shared_ptr<unsigned char>

有什么好的方法可以将shared_ptr转换为shared_ptr吗?我想到了以下但它看起来不是很干净。intmain(intargc,char**argv){std::shared_ptrp1=std::make_shared();std::shared_ptrp2=std::shared_ptr(reinterpret_cast(p1.get()),[p1](unsignedchar*){});} 最佳答案 你正在做的事情有一个现成的功能,reinterpret_pointer_cast:std::shared_ptrp2=st