草庐IT

const-ref

全部标签

c++ - 将 `hana::string` 转换为 `constexpr const char (&)[]`

我有一些旧代码使用了与str_const非常相似的东西描述here和here做一些constexpr字符串操作。str_const是ScottSchurr描述的文字类型,可以从字符串文字构造,因为它具有来自constchar(&)[]的模板构造函数.我现在也有一些使用boost::hana的新代码.我希望能够参加hana::string并创建一个str_const那是指它。最简单的方法是转换hana::string到constexprconstchar(&)[].(实际上,在这一点上这不是最简单的方法,最简单的方法肯定是在我的str_const实现中添加一个新的模板构造函数。但在这一点

c++ - 在类声明中初始化 const 成员变量时 Debug模式下的异常

#include#include#include#includeclassX{public:X(){std::cout>ValidatorType;constValidatorTypem_validators=ValidatorType{{"some-string",[](){//validationcodestd::cout以上代码使用Xcode7.2.1和Clang7.0.2在OSX上以调试和Release模式成功构建和运行。它还使用VisualStudioExpress2013forWindowsDesktop在Windows7上以Release模式成功构建和运行。但是,在Win

c++ - const、span 和迭代器问题

我尝试编写一个迭代器,通过索引遍历容器。It和一个constIt两者都允许更改容器的内容。Const_it和一个constConst_it两者都禁止改变容器的内容。在那之后,我尝试写一个span在一个容器上。对于类型T这不是const,两者都是constspan和span允许更改容器的内容。两者constspan和span禁止改变容器的内容。代码无法编译,因为://*thisisconstwithinaconstmethod//ButItrequiresanon-const*thishere.//SothecodedoesnotcompileItbegin()const{returnI

c++ - 避免接受 lambda 的方法的 const/non-const 重复

classFrame表示像素类型为P的图像.由于底层数据缓冲区格式的多种灵active,遍历其像素的算法并非易事。template//Pispixeltype;RM=is_row_majorclassFrame{//...templatevoiditerate(Ff){//iterateinawaythatisperformantforthisbufferif(stride==(RM?size.w:size.h)){auton=size.area();for(index_tk=0;k(stride)*(RM?size.h:size.w);for(index_tk0=0;k0我希望能够同

c++ - 需要帮助调试从 const char* 到 char* [-fpermissive] 的无效转换

我是c++的新手,不知道为什么会这样......第105行我收到此错误从constchar*到char*的无效转换[-fpermissive]第113行我收到从âconstchar*到char*[-fpermissive]的无效转换错误#include#include#includeusingnamespacestd;characWordWrap[1024];characPrint[1024];BasicConsole::BasicConsole(char*szName):ZFSubSystem(szName){m_iMaxWidth=50;//TEXT_MAX_LENGHT;m_bL

c++ - 在自定义 const native C++ 容器类上支持 "for each"

我想实现一个简单的nativeC++固定容量数组模板类,为了方便起见支持基于范围的“foreach”语法,开销最小。我在const实例上支持它时遇到问题。有了这个实现:templateclassList{public:List(){mSize=0;}constT*begin()const{returnmItems;}constT*end()const{returnmItems+mSize;}T*begin(){returnmItems;}T*end(){returnmItems+mSize;}private:size_tmSize;TmItems[Capacity];};和这种用法:c

c++ - Qt错误: 'const class QString' has no member named 'toStdString'

我收到此错误error:'constclassQString'hasnomembernamed'toStdString'虽然QString有它。(link).代码std::stringMessage::toStdString()const{returnm_string.toStdString();} 最佳答案 直接从这里复制答案:HowtoconvertQStringtostd::string?QStringqs;//EitherthisifyouuseUTF-8anywherestd::stringutf8_text=qs.toU

c++ - 通过 const 引用延长临时生命周期

C++我正在尝试了解const引用如何延长临时对象的生命周期。我正在运行oneoftheanswerstoWhatarethedifferencesbetweenpointervariableandreferencevariableinC++?中的代码片段并在VC11和g++4.8之间得到了冲突的结果。我在这里扩展了代码段:#includestructscope_test{~scope_test(){printf("scope_testdone!\n");}};intmain(){constscope_test&test=scope_test();printf("inscope\n")

c++ - 错误 C2662 无法从 const 转换为引用

这是我关于堆栈溢出的第一篇文章,我希望将来能加入社区。我正在为ADT类编写哈希表实现;我的大部分方法都在作业范围内达到了标准,但这让我很伤心。在这个我一直用来测试我编写的各种函数的测试应用程序中,我收到错误“errorC2662:'customer::getPhone':cannotconvert'this'ponterfrom'constcustomer'to'customer&'引用行“光标=find_ptr(entry.getPhone());”和“list_head_insert(data[hash(entry.getPhone())],entry);”我的函数实现代码如下:t

c++ - 带有仿函数修改对象的 const 函数

如果我们考虑以下方法,我的印象是bar不能修改this(即Foo的实例)。structFoo{inti;//varshallnotmodifytherespectiveinstanceofFoo,thus"const"voidbar(std::functionfunc)const{func(3);}};但是,以下是可能的:voidanothermethod(){Foof;f.bar([&](intx){f.i=3;});//modifyFoo.i"within"Foo::barconst.Dangerous?}我看到方法bar不是“直接”修改其实例的值i,而是通过给定参数“间接”修改函