草庐IT

is_const_method

全部标签

constexpr链接列表 - 从const x*到x*的无效转换

这是我创建一个简单的constexpr链接列表的尝试-structNode{constexprNode(constintn,Nodeconst*next=nullptr):value(n),next(next){}constexprNodepush(constintn)const{returnNode(n,this);}intvalue;Nodeconst*next;};constexprautogetSum(Noden){intsum=0;Node*current=&n;while(current!=nullptr){sum+=current->value;current=current->

c++ - const char * 值生命周期

这个问题在这里已经有了答案:Canalocalvariable'smemorybeaccessedoutsideitsscope?(20个答案)关闭7年前。编辑:重复标记中的链接问题已经回答了为什么这个问题中的代码有效的问题。关于字符串文字生命周期的问题在这个问题的答案中得到了回答。我试图了解constchar*指向的字符串如何以及何时被释放。考虑:constchar**p=nullptr;{constchar*t="test";p=&t;}cout离开内部范围后,我希望p成为指向constchar*的悬空指针。但是在我的测试中它不是。这意味着即使在t超出范围之后,t的值实际上仍然有效

c++ - STL 迭代器和 'const'

当我使用迭代器时,我遇到了一个问题,似乎是对const的某种隐式转换。我不太确定哪个代码是相关的(如果我知道我可能不会问这个问题!)所以我会尽力说明我的问题。typedefsetContainer;//notconstvoidLargeObject::someFunction(){//notconstContainer::iteratorit;//notconstfor(it=c.begin();it!=c.end();++it){//assumecisa"Container"(*it).smallObjectFunction();//notaconstfunction}}但是我总是得

c++ - 没有 () 的访问器,或对成员变量的 const 引用

我有兴趣创建一个我可以像这样使用的类classMyClass{vectorm_vec;public://Eitherthisconst&vectorvec;//Orsomeversionofthis.const&vectorgetVec(){returnm_vec};MyClass():vec(m_vec){}voidchangeVec(){m_vec.push_back(5);}}现在如果我想使用getVec(),语法有点麻烦:myClass.getVec()[5]我更希望能够以某种方式使用myClass.vec[5]不公开修改vector的能力。IE,我希望成员变量是私有(priv

C++ Linux : error: ‘move’ is not a member of ‘std’ how to get around it?

所以在我的VS2010上我可以编译如下代码:boost::shared_ptrinternal_thread;boost::packaged_taskinternal_task_w(boost::bind(&thread_pool::internal_run,this,internal_thread));internal_thread=boost::shared_ptr(newboost::thread(std::move(internal_task_w)));前两行在boost1.47.0和linux上没问题...但是在std::move上它给出了error:‘move’isnota

C++ const 语义引用

如果代码中有如下内容:func(constbase&obj)const语义是什么意思?这里的常数是什么?obj是对非常量对象的const引用还是对const对象的非常量引用? 最佳答案 没有“非常量”引用这样的东西,也就是说,引用总是绑定(bind)到同一个对象,而且没有办法改变它。"consttype&"表示对const类型的引用。 关于C++const语义引用,我们在StackOverflow上找到一个类似的问题: https://stackoverflo

c++ - 警告 : value computed is not used

为什么我在“BIO_flush(b64);”行收到警告消息“警告:未使用计算值”我怎样才能摆脱它?unsignedchar*my_base64(unsignedchar*input,intlength){BIO*bmem,*b64;BUF_MEM*bptr;b64=BIO_new(BIO_f_base64());bmem=BIO_new(BIO_s_mem());b64=BIO_push(b64,bmem);BIO_write(b64,input,length);BIO_flush(b64);BIO_get_mem_ptr(b64,&bptr);unsignedchar*buff=(u

c++ - 尝试使用 std::add_const 将 T& 转换为 const T&

我有一个T&,它有一个函数的const和非常量版本。我想调用该函数的const版本。我尝试使用std::add_const将T&转换为constT&但它不起作用。我做错了什么,我该如何解决?这是一个简单的例子。voidf(int&){std::cout::type>(r));}输出:int& 最佳答案 类型特征是解决这个问题的一种非常费力的方法。只需使用模板类型推导:voidf(int&){std::coutconstT&make_const(T&t){returnt;}intmain(){inta=0;int&r=a;f(make

C++11 统一初始化 : Field initializer is not constant

我正在尝试像这样实例化一组字符串:classPOI{public:...staticconststd::setTYPES{"restaurant","education","financial","health","culture","other"};...}现在,当我这样做时,我得到了这些错误(全部在这一行):error:fieldinitializerisnotconstantstaticconststd::setTYPES{"restaurant","education","financial","health","culture","other"};error:in-class

c++ - 使 std::unique<T> 与 std::unique<const T, CustomDeleterType> 兼容

在代码中,我为特定对象定义了3个std::unique_ptr指针类型:typedefstd::unique_ptrnonConstPtrDefaultDelete;typedefstd::unique_ptr>nonConstPtrCustomDelete;typedefstd::unique_ptr>ConstPtrCustomDelete;我遇到了一个用例,我需要将nonConstPtrDefaultDelete转换为ConstPtrCustomDelete并将nonConstPtrCustomDelete转换为ConstPtrCustomDelete。换句话说:nonConst