草庐IT

const_buffers

全部标签

c++ - 为什么 std::remove_const 不删除 const 限定符?

请注意,我使用std::thread只是为了在错误中获得可读类型:intmain(){constint*first;usingderef=decltype(*first);std::threads=std::remove_const::type{};//constint???std::threads2=deref{};//constintstd::threads3=std::remove_const::type{};//int}好像remove_const::type是constint,不可变int正如我所料。 最佳答案 注意*fi

c++ - 从非常量对象调用 const 函数

我需要从非常量对象调用const函数。看例子structIProcess{virtualbooldoSomeWork()const=0L;};classFoo:publicIProcess{virtualbooldoSomeWork()const{...}};classBar{public:constIProcess&getProcess()const{return...;}IProcess&getProcess(){return...;}voiddoOtherWork{getProcess().doSomeWork();}};打电话getProcess().doSomeWork();

c++ - 可以在堆栈上分配 const 静态字符串吗?

constchar*foo(){return"abcdef";}intmain(){printf("%s",foo());}符合标准的编译器可以决定在堆栈上分配"abcdef"吗?IE。标准中的什么强制编译器在.data部分中分配它? 最佳答案 来自C++规范§2.14.5/8的字符串文字;OrdinarystringliteralsandUTF-8stringliteralsarealsoreferredtoasnarrowstringliterals.Anarrowstringliteralhastype“arrayofncon

c++ - 何时在函数 args 中使用 const 和 const 引用?

在编写具有传递给它的args的C++函数时,根据我的理解,如果您可以保证对象不会被更改,则应始终使用const;如果指针不会更改,则应始终使用const指针。什么时候建议这种做法?什么时候使用const引用?例如,与仅通过指针传递它相比有什么优势?这个voidMyObject::Somefunc(conststd::string&mystring)如果一个字符串实际上已经是一个不可变对象(immutable对象),那么拥有一个const字符串有什么意义? 最佳答案 询问是否添加const是错误的问题,很遗憾。将非常量引用与传递非常量

c++ - 在 C 中允许重复的 const 限定符但在 C++ 中不允许?

示例代码片段constconstconstintx=10;intmain(){}在C中编译,但不在C++中。为什么它会在C中编译?我认为这在C中也会失败。没关系。C++标准的哪一部分禁止使用重复的const,而C标准的哪一部分允许这样做? 最佳答案 C99§6.7.3/4:Ifthesamequalifierappearsmorethanonceinthesamespecifier-qualifier-list,eitherdirectlyorviaoneormoretypedefs,thebehavioristhesameasif

const 指针的 C++ typedef 解释

首先,示例代码:案例一:typedefchar*CHARS;typedefCHARSconstCPTR;//constantpointertochars文字替换CHARS变为:typedefchar*constCPTR;//stillaconstantpointertochars案例2:typedefchar*CHARS;typedefconstCHARSCPTR;//constantpointertochars文字替换CHARS变为:typedefconstchar*CPTR;//pointertoconstantchars在案例2中,在文本替换CHARS后,typedef的含义发生

c++ - 来自 const std::vector<>& 的自动;对象还是引用?

假设我们有一个具有以下接口(interface)的对象:structNode_t{...conststd::vector&getChilds()const;}node;现在,我使用auto访问该属性。像这样的变量:autochilds=node->getChilds();childs的类型是什么??一个std::vector还是引用一个? 最佳答案 childs的类型将是std::vector.auto由与templatetypededuction相同的规则提供支持.此处选择的类型与为templatef(Tt);选择的类型相同在像f

C++ 构造函数 : garbage while initialization of const reference

这段代码有什么问题,为什么我得到错误的答案:classX{private:constinta;constint&b;public:X():a(10),b(20){//std::cout上面的代码会给我结果display():a:10display():b:1104441332但如果我删除默认构造函数中注释的2行,它会给我正确的结果,即constructor:a10constructor:b20display():a:10display():b:20请帮忙,谢谢 最佳答案 您正在将b初始化为对临时的引用。20的值被创建并且只存在于构造

c++ - Protocol Buffer - 存储双数组、1D、2D 和 3D

如何使用ProtocolBuffer存储双(1D)数组?多维(2D或3D)密集数组呢? 最佳答案 可以简单地模仿C/C++内存布局:messageDoubleMatrix{requireduint32rows=1;requireduint32cols=2;repeateddoubledata=3[packed=true];}要访问数据,请使用data[i*cols+j](row-major)或data[i+rows*j](column-major)。对于方阵,只需存储rows/cols之一。技术上即使在矩形情况下protobuf也会

c++ - 无法将参数 'std::basic_string<char>' 的 'const char*' 转换为 '1' 到 'int system(const char*)'

当我尝试编译我的脚本时,我收到此错误:“'constchar*'和'constchar[6]'类型的无效操作数到二进制'operator+'”。这里应该是错误:stringname="john";system("quickscan.exeresolution300selectscannerjpgshowuishowprogressfilename'"+name+".jpg'"); 最佳答案 表达式的类型"quickscan.exeresolution300selectscannerjpgshowuishowprogressfilen