草庐IT

const_assert

全部标签

c++ - __FILE__ 可以在 C++ 中被 const char* 引用吗?

阅读从http://www.almostinfinite.com/memtrack.html链接的memTrack库后我有一个问题__FILE__可以保存在类型为constchar*的变量中forever并且不需要使用strdup()?BlockHeader::Stamp不是使用strdup()来分配__FILE__字符串吗?voidBlockHeader::Stamp(charconst*filename,intlineNum,charconst*typeName){myFilename=filename;//don'tusestrdup(filename)toassign?myLi

c++ - "expected nested-name-specifier before ‘const’ 错误“在 g++ 中类型名称为 const

我在C++中有这段代码templateclassDD:publicenumerables{...private:typenameconstDD&mContainer;}它给了我两条错误信息:错误:在“const”之前需要嵌套名称说明符错误:“&”标记前的声明符无效typenameconst有什么问题?代码?它使用MSVCC++编译得很好。已添加typenameDD&constmContainer;和consttypenameDD&mContainer;给我同样的错误。 最佳答案 那么,typename在那里做什么?您指的不是嵌套类型

c++ - 为什么 const 限定符不能作用于 const 对象的指针成员?

我知道这已经被问了很多,但我能找到的唯一答案是当使用(int*)或类似的东西实际上抛弃了const-ness时。当不涉及强制转换时,为什么const限定符不能作用于const对象上的指针类型成员变量?#includeclassbar{public:voiddoit(){std::coutdoit()\n";mybar1->doit();//Thiscallsbar::doit()insteadofbar::doit()conststd::cout上面的代码产生以下输出(在gcc4.5.2和vc100中测试):foo::doit()constcallingmybar1->doit()ba

c++ - "const"是如何实现的?

C或C++编译器(例如gcc)如何遵守const声明?例如,在下面的代码中,编译器如何跟踪变量ci是const并且不能被修改?intget_foo(){return42;}voidtest(){inti=get_foo();i+=5;constintci=get_foo();//ci+=7;//compileerror:assignmentofread-onlyvariable?ci?} 最佳答案 很像变量的任何其他符号信息(地址、类型等)。通常有一个符号表存储有关已声明标识符的信息,每当遇到对该标识符的另一个引用时都会查询它。(一

c++ - 在 C++ 中更改 const 值的方法

constvalue在编程中经常使用。它为程序员提供了安全性,即该值将来不会改变。不幸的是,这并没有完全执行。因此,它有时会导致难以破译的细微错误。举个例子:inta=1;constint&b=a;//IpromiseIwillnotchangea=3;//Iamaliar,Iamnow3cout也可以用其他方式改变const的值;其中一些非常复杂,需要大量代码,而这些代码通常会在其他代码段中丢失,从而导致错误。因此,有人会详细说明更改const值的可能方法吗?如果您不了解所有内容,请不要担心,很多人不会(包括我自己)。把你知道的所有方法都说出来——毕竟你只能给出你所拥有的!

C++在派生类中初始化基类的const int?

我的基类中有一个常量int变量,我想在我的派生类中用不同的值(作为参数)初始化响应变量,这可能吗?这是我做的://Base.h(methodsimplementedinBase.cppintheactualcode)classBase{public:Base(constintindex):m_index(index){}intgetIndex()const{returnm_index;}private:constintm_index;};//Derived.hclassDerived:publicBase{public:Derived(constintindex,conststd::s

c++ - 强制 foreach 使用 const 迭代器

我有一个自定义容器类,它实现了cbegin()和cend()函数。然后我在foreach循环中使用它,但它似乎需要begin()和end()成员函数,即使我尝试使用const修饰符:for(constautoval:container)像这样:for(autoconstval:container)像这样:for(constautoconstval:container)是否可以强制foreach使用常量c函数? 最佳答案 当然:让范围看起来像一个const范围:templateTconst&make_const(Tconst&argu

c++ - 如何在 Visual Studio 2013 的 Debug模式下关闭 ASSERT

有什么方法可以关闭断言而不是切换到Release模式。我需要调试经常进行断言的代码,这会减慢我的工作速度。这些断言与我要解决的问题无关,所以现在它们只会减慢我的进度,因为它们在我的一个基类中经常被调用。现在我没有时间改进他们的设计,所以有人可以告诉我是否有办法在Debug模式下关闭断言并使用它的功能。 最佳答案 用户_CrtSetReportModeintiPrev=_CrtSetReportMode(_CRT_ASSERT,0);//StartOperationwithnoASSERTs...//Restorepreviousmo

c++ - 如何将 const char* 转换为字符串,然后再转换回 char*?

我刚开始使用C++,很难理解constchar*。我正在尝试将方法中的输入转换为string,然后更改字符串以在我想要的位置添加连字符并最终获取该字符串并将其转换回char*返回。到目前为止,当我尝试这个时,它给我一个总线错误10。char*getHyphen(constchar*input){stringvowels[12]={"A","E","I","O","U","Y","a","e","i","o","u","y"};//convertchar*tostringstringa;inti=0;while(input!='\0'){a+=input[i];input++;i++;}

c++ - 为什么复制 const shared_ptr& 不会违反 const-ness?

尽管我的代码编译得很好,但这一直困扰着我,我无法在stackoverflow上找到答案。以下通用构造函数是将shared_ptr传递给构造函数中的类实例的一种方法。MyClass{MyClass(conststd::shared_ptr&pt);std::shared_ptrpt_;//EDITED:Removed&typo};MyClass::MyClass(conststd::shared_ptr&pt):pt_(pt){}这编译得很好。我的问题如下:在我的理解中,像这样声明一个参数const:voidmyfunc(constT&t)promise不改变t。但是,通过将shared