草庐IT

const_buffers

全部标签

c++ - 给出编译错误的 const 对象的 vector

我在我的代码中声明了以下内容vectormylist;我得到以下编译错误-new_allocator.h:75:error:`const_Tp*__gnu_cxx::new_allocator::address(const_Tp&)const\[with_Tp=constA]'and`_Tp*__gnu_cxx::new_allocator::address(_Tp&)const[with_Tp=constA]'cannotbeoverloaded但如果声明-vectormylist;我的代码编译了。在这种情况下不允许使用const吗?我在这里复制我的代码供大家引用-#include#

c++ - 作为 const 和通过引用传递 - 值得吗?

这个问题在这里已经有了答案:10年前关闭。PossibleDuplicate:HowtopassobjectstofunctionsinC++?在我的游戏中,我过度使用数学vector和运算符重载它们。classVector{floatx,y;};这基本上是关于我的Vector类(不包括方法)。我不是C++专家,我已经看到并阅读了有关作为常量传递和通过引用传递的信息。那么,下面的代码示例中的性能差异在哪里?FloatRandomCalculation(Vectora,Vectorb){returna.x*b.x/b.x-a.x*RANDOM_CONSTANT;}//versus..Fl

c++ - 按值传递与 const & 和 && 重载

因此,在查看move语义后,我发现一般共识是在您打算转移所有权时按值传递。但是在ScottMeyer'stalkonUniversalreferences我注意到std::vector::push_back有2个重载:voidpush_back(constT&value);voidpush_back(T&&value);所以我想,voidpush_back(Tvalue);还不够吗?我问了几个人,最终得出了以下测试用例:#include#include#includestructA{A(){std::coutcontents;A&alias=*reinterpret_cast(&con

c++ - Protocol Buffer 精简版与常规 Protocol Buffer

我一直在研究c++序列化框架将占用更少的空间和良好的性能。我找到了这个帖子c++networkserialization这基本上建议使用ProtocolBuffer的精简版。从thispage不清楚lite版本的具体功能是什么我的问题是;坚持使用protocolbufferslite会丢失哪些功能? 最佳答案 “lite”版本无法序列化到或从iostream,或“FileDescriptor”,不能使用Reflection特征(尽管itdoesuserefection),以及......其他特征的分散。我的建议是只使用精简版,直到遇

c++ - 为什么在 const 成员结构中需要构造函数?

我有一个类似这样的代码:classAClass{public:structAStruct{};AClass(){}private:constAStructm_struct;};intmain(){AClassa;}它抛出这个编译错误(ClangLLVM5.1版):error:constructorfor'AClass'mustexplicitlyinitializetheconstmember'm_struct'如果我为structAStruct指定C++11默认构造函数,我会得到同样的错误:structAStruct{AStruct()=default;};但是,这可以通过编写一个空

c++ - 从 'const char*' 到 'char*' 的无效转换

有一个如下所示的代码。我在传递参数时遇到问题。stringstreamdata;char*addr=NULL;strcpy(addr,retstring().c_str());retstring()是一个返回字符串的函数。//morecodeprintfunc(num,addr,data.str().c_str());我得到了错误invalidconversionfrom'constchar*'to'char*'.initializingargument3of'voidPrintfunc(int,char*,char*)'onargument3ofthefunction在上面一行。函数

c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的

将指向非常量的指针转换为指向常数的指针是合法的。那么为什么将指向非const的指针转换为指向const的指针是不合法的呢?例如,为什么下面的代码是非法的:char*s1=0;constchar*s2=s1;//OK...char*a[MAX];//akachar**constchar**ps=a;//error! 最佳答案 来自标准:constcharc='c';char*pc;constchar**pcc=&pc;//notallowed*pcc=&c;*pc='C';//wouldallowtomodifyaconstobjec

c++ - 使用 decltype 获取表达式的类型,没有 const

考虑以下程序:intmain(){constinte=10;for(decltype(e)i{0};i使用clang(以及gcc)编译失败:decltype.cpp:5:35:error:read-onlyvariableisnotassignablefor(decltype(e)i{0};i基本上,编译器假设i必须是const,因为e是。有没有办法我可以使用decltype来获取e的类型,但删除const说明符? 最佳答案 为此,我更喜欢autoi=decltype(e){0};。它比使用type_traits简单一些,而且我觉得

c++ - const_iterators 更快吗?

我们的编码指南更喜欢const_iterator,因为它们比普通的iterator快一点。当您使用const_iterator时,编译器似乎会优化代码。这真的正确吗?如果是,那么内部究竟发生了什么使const_iterator更快?。编辑:我写了一个小测试来检查const_iterator与iterator并发现不同的结果:迭代10,000个对象const_terator减少了几毫秒(大约16毫秒)。但并非总是。有一些迭代是相等的。 最佳答案 如果没有别的,const_iteratorreads更好,因为它告诉任何阅读代码的人“我只

c++ - 从函数返回对局部变量的 const 引用

我有一些关于从函数返回对局部变量的引用的问题:classA{public:A(intxx):x(xx){printf("A::A()\n");}};constA&getA1(){Aa(5);returna;}A&getA2(){Aa(5);returna;}AgetA3(){Aa(5);returna;}intmain(){constA&newA1=getA1();//1A&newA2=getA2();//2A&newA3=getA3();//3}我的问题是=>getA1()的实现是否正确?我觉得这是不正确的,因为它返回的是局部变量或临时变量的地址。main(1,2,3)中的哪些语句会