草庐IT

constness

全部标签

c++ - 'this' 参数的类型为 const 但函数未标记为 const

好的,所以我在C++方面有点菜鸟,在我的第二个作业中,我需要使用公共(public)和私有(private)参数等创建类。基本上,mutator函数不起作用,因为它们显然不是输入常量?这是带有类的头文件:classCustomer{private:stringPhoneNumber_;stringName_;stringAddress_;public:stringget_PhoneNumber()const{returnPhoneNumber_;}//Accessorconstvoidset_PhoneNumber(unsignedx){PhoneNumber_=x;}//Mutato

c++ - T*... 和 const T& 的部分排序

cppreference声明如下templateintf(T*...);//#1templateintf(constT&);//#2f((int*)0);//OK:selects#1//(wasambiguousbeforeDR1395becausedeductionfailedinbothdirections)如果我们关注DR1395我们看到了IfAwastransformedfromafunctionparameterpackandPisnotaparameterpack,typedeductionfails.Otherwise,usingUsingtheresultingtype

c++ - 将 std::pair<T1, T2> const 转换为 std::pair<T1 const, T2> const 安全吗?

reinterpret_cast是否安全(理论上或实际上)一个std::pairconst&变成std::pairconst&,假设程序员没有故意做一些奇怪的事情,比如专门std::pair? 最佳答案 这样做并不便携。std::pair要求在第20.3节中列出。第17.5.2.3条阐明了Clauses18through30andAnnexDdonotspecifytherepresentationofclasses,andintentionallyomitspecificationofclassmembers.Animplemen

c++ - static const 对命名空间成员有什么影响

//MyClass.hnamespaceMyNamespace{staticconstdoubleGasConstant=1.987;classMyClass{//constructors,methods,etc.};}我之前在MyClass声明中声明了GasConstant(并且在源文件中有单独的定义,因为C++不支持非整数类型的const初始化)。但是,我需要从其他文件访问它,而且从逻辑上讲,它似乎应该驻留在命名空间级别。我的问题是,staticconst在这种情况下有什么作用?显然const意味着我不能为GasConstant分配新值,但是命名空间中的静态成员是什么意思。这是否类

c++ - 为什么 C 不允许从 char ** 到 const char *const * 的隐式转换(而 C++ 允许)?

我知道从char**到constchar**的隐式转换无法完成以及为什么,以及到char*const*有效。请参阅底部的解释链接。除了一件特定的事情之外,这一切都是有意义的。所以我有以下代码:#includevoidprint(constchar*const*param){printf("%s\n",param[0]);}intmain(intargc,char**argv){print(argv);return0;}如果我将它编译为C++代码,它编译得非常好。但是,如果相同的代码仅编译为C代码,我会收到一个错误(好吧,一个警告,但我们假设-Werror,即将警告视为错误)。gcc:t

c++ - 将 'const' 添加到指针可以帮助优化吗?

我有一个指针int*p,在循环中做一些操作。我不修改内存,只是读取。如果我将const添加到指针(两种情况下,constint*p和int*constp),它可以帮助编译器优化代码?我知道const的其他优点,例如安全性或self文档,我询问了这个特殊情况。重新表述这个问题:const可以给编译器任何有用的(用于优化)信息吗? 最佳答案 虽然这显然是特定于实现的,但很难看出将指针从int*更改为intconst*可以提供任何附加信息,否则编译器不会知道。在这两种情况下,指向的值都可以在循环执行期间发生变化。因此它可能不会帮助编译器优

c++ - 声明和定义之间 const 限定符的使用不一致

我注意到在函数声明中存在的值参数上可以有const限定符,然后在定义中省略。这不会改变函数的签名。它实际上编译得很好。我还注意到常规类和模板类之间的行为不同。在GCC和Clang中的处理方式也有所不同。考虑以下代码:templatestructA{voidf(constint);};templatevoidA::f(intx){x=0;}structB{voidf(constint);};voidB::f(intx){x=0;}voidf(){Aa;a.f(0);Bb;b.f(0);}当我使用GCC编译时,我没有收到任何错误。使用Clang我得到:test.cpp:10:7:error

c++ - 在 Mac OS 上将 Xcode 更新到最新版本后 "warning: section "__const_coal "is deprecated"错误

在我将Xcode更新到最新版本并接受许可协议(protocol)之前,我的C++程序的g++编译器工作正常。我还尝试使用clang而不是g++进行编译,但出现错误。现在我收到一长串错误。有人知道出了什么问题吗?Ivans-MacBook-Pro:CS6771A3-GenericDirectedWeightedGraphivanteong$g++-std=c++14-Wall-Werror-O2-otest6tests/test6.cpp/var/folders/3d/hqly97ld37b1kd6wx9gjn2tc0000gn/T//ccZfBPvE.s:1:11:warning:se

c++ - 为什么指针 vector 不能转换为 const 指针的 const vector ?

类型vector不能转换为constvector.例如,以下给出编译错误:#includeusingnamespacestd;voidfn(constvectorcvcc){}intmain(){vectorvc=vector();fn(vc);}我明白为什么vector不能转换为vector-constchar*类型的额外成员可以添加到vector中,之后它们可以作为非常量访问。但是,如果vector本身是const,则不会发生这种情况。我最好的猜测是这将是无害的,但编译器无法推断这是无害的。如何解决这个问题?这个问题是由C++FQAhere提出的。. 最

c++ - const 到非 const 迭代器比较,它们是否有效

这个问题在这里已经有了答案:Iscomparisonofconst_iteratorwithiteratorwell-defined?(3个回答)关闭4年前。我在一个容器中有两个迭代器,一个是常量,一个是非常量。比较它们以查看它们是否都引用容器中的同一个对象是否存在问题?这是一个通用的C++11迭代器问题:Canaconstandnon-constiteratorbelegitimatelycomparedtoseeiftheybothrefertothesameobject,independentofthetypeofcontainer(i.e.,theyarebothiterato