草庐IT

const-ref

全部标签

c++ - 转换 Const 指针以使用遗留 C 代码

作为我之前问题(VariableLengthArrayPerformanceImplications(C/C++))的后续,我在使用C系统调用writev()维护const正确性时遇到了一些麻烦。也就是说,尽管我使用的是C++,但我似乎遇到了与该用户在C中遇到的完全相同的问题:https://codereview.stackexchange.com/questions/9547/casting-const-pointer-to-non-const-pointer-when-using-struct-iovec这是我的代码片段:intmy_awesome_transmit_functio

c++ - ctor 声明/定义中接受的 const 限定符(llvm 错误?)

我的编译器(实际上是AppleLLVM5.0版(clang-500.2.79)(基于LLVM3.3svn))接受(编译)该代码:classX{private:inti;public:constX(){cout它的工作方式就好像没有const限定符引导ctor定义一样。我尝试了-Wall、-pedantic不同类型的标准激活,总是一样的......所以:我错过了什么吗?我没能发现它在最新标准中的句法是正确的……这是gcc/llvm的错误吗?似乎gcc/llvm默默地忽略了const。这是我错过的功能吗?我的示例无法证明它的用处吗?注意:gcc3.4.3不编译它,gcc4.4.5也不编译。

c++ - 在方法/函数中使用 `static const std::string` 还是只使用 `const std::string` 更好?

我有一个方法/函数:voidfoo(){staticconststd::stringstrSQLQuery="SELECT...";//ormaybeconststd::stringstrSQLQuery="SELECT...";//someoperationsonstrSQLQuery//i.e.concatenatingwithWHERE,etc.:conststd::stringstrSQL=strSQLQuery+strWHERE;doSthOnDataBase(strSQL);}(SQL只是一个例子)staticconst只会被初始化一次,但会一直保存在内存中直到进程结束。c

c++ - 为什么未初始化的 const 成员在 C 中的处理方式与在 C++ 中的处理方式不同?

这个问题在这里已经有了答案:constinCvsconstinC++(5个答案)关闭7年前。我为要链接的库包含一个Cheader。header有一个结构foo,我想在其中声明一个新变量bar。我收到编译器错误:error:uninitializedconstmemberin"structfoo"这些成员需要初始化是有道理的,因为以后不能为它们赋值。但是使用这个库的C程序可以做完全相同的事情并且它可以工作。C和C++标准有区别吗?这只是一个示例。实际上,我指的是libmtd.h(mtd-utils)中的结构mtd_dev_info。http://mtd-utils.sourcearchi

c++ 为什么 const& 可以得到 const 值?

这个问题在这里已经有了答案:WhathappenswhenIassignatemporaryinttoaconstreferenceinC++?[duplicate](2个答案)关闭8年前。我明白为什么int&x=1是非法的(你不能引用常量值),但我不明白为什么constint&x=1合法吗?你怎么能引用数字“1”?它甚至不是定义的变量。编辑:我阅读了这篇文章中给出的答案:WhathappenswhenIassignatemporaryinttoaconstreferenceinC++?但有人可以解释他的意思是“表达式f(1)返回的临时值的生命周期将延长其生命周期。此规则对于const

c++ - 对 'cv::viz::Viz3d::Viz3d(std::string&const)' 的 undefined reference

我已经使用qtcreator运行了我的opencv代码,当我尝试使用Viz库时得到了这个答案。代码:#include#include#include#include#include///Createawindowviz::Viz3dmyWindow("VizDemo");///StarteventloopmyWindow.spin();///Eventloopisoverwhenpressedq,Q,e,Eprintf("Firsteventloopisover\n");///Accesswindowviaitsnameviz::Viz3dsameWindow=viz::getWind

c++ - 按值或 const 引用传递函数?

我应该按值还是按对一个函数的引用来传递std::string。此函数将此值存储在类的成员变量中。当谈到值传递或引用传递时,我总是感到困惑。请消除我对此的困惑。代码如下:classDataStore{public:voidaddFile(conststring&filename,constset&filePaths){if(dataStoreMap.insert(make_pair(filename,filePaths)).second){cout>dataStoreMap;};我应该像这样声明函数吗:voidaddFile(conststring&filename,constset&f

c++ - 为什么 const 类成员必须是静态的才能得到适当的优化?

给定:classFoo{constintx=5;public:inlineintget(){returnx;}};classBar{staticconstintx=5;public:inlineintget(){returnx;}};intfn0(Foo&f){returnf.get();}intfn1(Bar&b){returnb.get();}编译后的输出提供内存获取以读取fn0()中x的值,而添加static结果是文字5被内联到fn1()中。这意味着只有当整数常量为静态时,get()的调用方才可以像使用常量代替get()一样进行优化。我有更复杂的情况,其中static不合适。派生

c++ - 自动转换模板<T>到模板<const T>

下面的这段应该主要用于T={char,constchar}的字符串View是主要的预期模板实例化目标。cmp函数应该类似于strcmp来比较View。.问题是虽然char*愉快地转换为constchar*我不知道如何获得SVec转换为SVec一样开心。最后一行(cout)无法编译。我必须明确地进行转换(cmp(SVec(rv),rvc))。它可以像char*一样自动吗?至constchar*?代码(大大简化):templateclassSVec{protected:T*begin_;size_tsize_;public:SVec(T*begin,size_tsize):begin_(b

c++ - 为什么 static const char * template struct 成员没有初始化

我有一些C++11模板代码,我正在尝试移植到VisualC++Compiler2015。原始代码工作得很好,但是我需要重写它以解决constexpr的问题。Theoriginalcode(simplifiedexample)#includestructString{staticconstexprconstchar*value{"STRING"};};templateclassDerived{public:staticconstexprconstchar*value{Base::value};};templatestructFoo{staticconstexprconstchar*val