草庐IT

remove_const

全部标签

c++ - 通过 const 获取参数并通过 const 返回的函数的生命周期延长

在C++中,当你有以下情况时:std::stringget_string();std::stringconst&v=get_string();从get_string()返回的临时对象的生命周期延长为与引用v相同的生命周期;如果我有以下内容:std::stringconst&get_string(std::stringconst&p){returnp;}std::stringconst&v=get_string(std::string{"Hello"});临时的生命周期是否延长?或者这是悬空引用;我的理解是临时绑定(bind)到p的生命周期并且它只在函数的持续时间内存在,并且对临时对象的

c++ - 如何分配一个 std::pair 其中一个组件类型为 const?

我正在尝试编写一个与std::map兼容的关联容器。为此,我必须创建一个插入方法,该方法以std::pair的形式接受一个新项,其中第一个组件是const类型。例如:std::pairp.我遇到的问题是这样的对象不能分配给另一个对象。因此,在我的MapCompatibleContainer的内部代码中,我无法将新对复制到私有(private)变量(std::vector)。我该如何解决这个问题?谢谢 最佳答案 正如您所说,您不能分配给const对象。标准容器通过分配原始内存并就地构造对象来解决这个问题。复制构造仍然有效。此外,关联容

c++ - 字符串隐式转换运算符到 const char*/wchar_t *

我发现MFC/ATLCString类在Win32C++代码中非常方便;特别是我发现我们可以将CString的实例直接传递给Win32API的LPCWSTR(即constwchar_t*)参数很方便,感谢CString定义的隐式转换运算符。相反,当使用std::wstring时,需要显式调用.c_str()方法。那么,为什么STL字符串类(std::string和std::wstring)需要显式方法调用(c_str())而不是定义一个隐式的constchar*/constwchar_t*转换运算符?隐式转换运算符是否隐藏着严重的陷阱? 最佳答案

c++ - 重载 ‘pow(const double&, double)’的调用不明确

我是C++的新手,这是我的问题:我需要这个数量:h=pow(mesh.V()[i0],1.0/3);但是每次编译程序时都会收到此错误消息:callofoverloaded‘pow(constdouble&,double)’isambiguous如果我写doubleV=mesh.V()[i0];h=pow(V,1.0/3);我得到:callofoverloaded‘pow(double&,double)’isambiguous现在我想我明白了constdouble&和double&指的是什么,但是我怎样才能转换constdouble&到double?谢谢! 最

c++ - const 引用在 C++ 中是否有外部链接?

根据C++1998标准第3.5节的第3条,const引用具有内部链接。Anamehavingnamespacescope(3.3.5)hasinternallinkageifitisthenameofanobject,reference,functionorfunctiontemplatethatisexplicitlydeclaredstaticor,anobjectorreferencethatisexplicitlydeclaredconstandneitherexplicitlydeclaredexternnorpreviouslydeclaredtohaveexternall

c++ - 将 const char** 与模板特化结合使用

我正在尝试为返回数值数组的最大值(通用版本)或C字符串数组的最长C字符串(特化)的函数编写模板特化。如果我不使用const-ness,我的函数原型(prototype)看起来像这样templateTmaxn(T*my_Tptr,unsignedintn);templatechar*maxn(char**my_cstrings,unsignedintn);并且我的代码可以编译。但是,如果我尝试使用const-ness,我的函数原型(prototype)将如下所示,templateTmaxn(constT*my_Tptr,unsignedintn);templatechar*maxn(co

c++ - Qt 5 : const char * QString cast

在Qt4中,可以自动将QString转换为“constchar*”,例如我可以将QString传递给需要“constchar*”的函数。voidmyFunction(constchar*parameter);QStringmyString;myFunction(myString);//worksinQT4然而,在Qt5中,我会收到“错误C2440:‘typecast’:无法从‘QString’转换为‘constchar*’”(这是VisualC++2008编译器,其他编译器会抛出类似的东西)。如果我对文档的理解正确,那是因为QT5中不再包含Qt3兼容层。当然,我可以更改函数调用myFu

c++ - 使用 C++11 auto 作为 const 函数对象的返回类型

我有一个const函数对象,暂时它返回void。但可以返回int或double。我正在用c++11风格编写代码,只是想使用auto作为返回类型。尽管代码可以编译,但我不确定它是否100%正确。这是代码。templatestructmy_func{public:my_func(){}my_func(graph_t&_G):G(_G){}templateautooperator()(edge_tedge)->voidconst{//dosomethingwiththeedge.}//operatorprivate:graph_t&G;};//callthefunctor:(passgrap

c++ - 二进制表达式 ('const' 和 'const' 的无效操作数)

这个问题在这里已经有了答案:HowcanIusestd::mapswithuser-definedtypesaskey?(8个答案)关闭6年前。我是一个C++菜鸟,所以如果你觉得这个问题很傻,请不要介意我在下面用C++声明map:std::map>>radioMap;完整代码:不知道,但使用下面的代码,我可以解决我的问题classRadioMap:publicstd::iterator_traits,publicCloneable{private:std::map>*>radioMap;std::vectorradioDevices;public:voidadd(constCartes

c++ - 采用 const 参数的默认移动构造函数

定义类时,以下是否有效?T(constT&&)=default;我正在阅读移动构造函数here并且它解释了如何仍然可以隐式声明默认值:Aclasscanhavemultiplemoveconstructors,e.g.bothT::T(constT&&)andT::T(T&&).Ifsomeuser-definedmoveconstructorsarepresent,theusermaystillforcethegenerationoftheimplicitlydeclaredmoveconstructorwiththekeyworddefault.在页面底部,它提到了缺陷报告CWG2