草庐IT

const_cast-ed

全部标签

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++ - 带有显式右值转换运算符的 static_cast

我正在编写一个简单的包装类,我想为包装类型提供显式转换运算符。以下代码使用gcc编译良好classwrap{doublevalue;public:explicitwrap(doublex):value(x){}explicitoperatordouble&&()&&{returnstd::move(value);}};intmain(){wrapw(5);double&&x(std::move(w));//okdouble&&y=static_cast(std::move(w));//clangreportsanerrorhere}但是clang报告error:cannotcastfr

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++ - 为什么使用reinterpret_cast 将char* 转换为结构似乎可以正常工作?

人们说相信reinterpret_cast将原始数据(如char*)转换为结构是不好的。例如,对于结构structA{unsignedinta;unsignedintb;unsignedcharc;unsignedintd;};sizeof(A)=16和__alignof(A)=4,完全符合预期。假设我这样做:char*data=newchar[sizeof(A)+1];A*ptr=reinterpret_cast(data+1);//+1istoensureitdoesn'tpointsto4-bytealigneddata然后复制一些数据到ptr:memcpy_s(sh,sizeo

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

c++ - 类型的模板特化和 const 限定符

为什么编译器期望模板在(具体)类型上单独特化T和constT?让我举个例子。我有一个由类类型键控的无序映射Keystd::unordered_mapdata;要编译它必须专门化std::hash在类型上Key作为namespacestd{templateclasshash{/*implementation*/};}但是,当我将map类型更改为std::unordered_mapdata;编译器没有使用我的特化,而是选择了通用的std::hash,这只不过是一个编译时断言,直到我专门化std::hash.撇开使用const限定映射键类型的实用程序,为什么不constT折叠到T在寻找这种情

c++ - boost::lexical_cast<signed char> 无法处理负数?

这个简短的C++程序的行为方式让我感到困惑:#include#include#include#includeintmain(void){signedcharc=-2;assert(c==-2);c=boost::lexical_cast(std::string("-2"));std::cout使用g++5.2.1和boost-1.58.0,我得到:terminatecalledafterthrowinganinstanceof'boost::exception_detail::clone_impl>'what():badlexicalcast:sourcetypevaluecouldn