草庐IT

const_set

全部标签

c++ - 对于 malloc() 失败是否有等效的 set_new_handler()?

在C++中,你can安排一个函数在new失败时调用。有没有办法在malloc失败时调用一个函数?假设正在从我不想更改的第三方库调用malloc。我认为没有可移植的答案,所以我很乐意接受特定于平台的答案。我在某些平台上使用Linux+uclibc,在其他平台上使用Linux+glibc。我打算使用setrlimit限制malloc可以返回的内存量。 最佳答案 malloc如果失败则返回NULL。您应该处理它,以及CRT内存函数的其他故障(realloc尤其容易出错)。在一般情况下,我认为您必须将内存的所有CRT使用包装在您自己设计的函

c++ - 在 C/C++ 中初始化结构的 const 成员...取决于编译器?

最近我在使用BorlandC++5.2的遗留环境中遇到编译器错误。我有一个.cpp文件,其中包含来自某些我无法控制的C源代码的header。header包含一个包含const成员的结构定义,编译器提示“类中没有构造函数的常量成员”。经调查,此错误似乎与编译器相关。下面是一些带有各种编译器结果的示例代码:#includetypedefstruct{constfloata;}_floater;intmain(){_floaterf={5.1F};printf("%f\r\n",f.a);return0;}Borland5.2E:\Projects\Scratchpad>bcc32-Pcon

c++ - 在 unordered_set 中插入一个新元素 : should the hint be end()?

如果我确定某个值还没有进入unordered_set,并且我要插入这样的值,传递这个集合end()是否正确>迭代器作为提示?编辑:代码:#includeusingnamespacestd;unordered_setsomeset;intmain(){autoit=someset.find(0);if(it==someset.end())someset.insert(it,0);//correct?possibleperformanceboostifthesetisactuallypopulated?} 最佳答案 我想,你可以简单地调

c++ - 如何克服 make_shared constness

我遇到了一个问题,无法决定正确的解决方案是什么。下面是用于说明的代码示例:#include#includeclassTestClass{public:inta;TestClass(int&a,intb){};private:TestClass();TestClass(constTestClass&rhs);};intmain(){intc=4;boost::shared_ptrptr;//NOTE:twostepinitializationofsharedptr//ptr=boost::make_shared(c,c);//(newTestClass(c,c));}问题是我无法创建sh

c++ - Getter 函数的 Const 正确性

这是一个关于const正确性的简单问题。我有这门课:templateclassFoo{public:std::mapmembers;templatestd::vector&member(conststd::string&memberName){returnboost::any_cast&>(members[memberName]);}};然后我有一个包含以下内容的仿函数:booloperator()(Foo&foo)const{std::vector&member=foo.member(_memberName);这里让我感到困惑的是我不能通过引用const来传递Foo,因为我正在调用非

c++ - 如何理解函数ostream& operator<< (ostream& os, const unsigned char* s)

对于像这样的函数声明ostream&operator我想知道返回了什么。CPP引用说它返回ostream对象。但为什么它是ostream&而不是简单的ostream?谢谢 最佳答案 运算符返回ostream&(即对ostream对象的可修改引用)而不是拷贝或void的原因是它允许链接,因为实例,以std::cout作为ostream对象的常见示例:unsignedinti=2;std::cout这里我们链接了两个constchar*,一个unsignedint和一个流修饰符,而不必用单独的行将它们分开,这使得阅读和明白了。

c++ - 对临时对象的 const 引用在函数作用域(生命周期)后被破坏

在询问时thisquestion,我了解到对临时对象的const引用在C++中是有效的:intmain(){inta=21;intb=21;//error:invalidinitializationofnon-constreference//int&sum=a+b;e[...]//OKintconst&sum=a+b;returnsum;}但在下面的例子中,常量引用refnop指的是一个被销毁的临时对象。我想知道为什么?#include#includestructA{//datastd::mapm;//functionsconstA¬hing()const{return*this

c++ - C++ : why does free not accept a const void*, 中的 malloc/free 有更好的方法吗?

这个问题在这里已经有了答案:UnabletofreeconstpointersinC(12个答案)关闭8年前。将C++11代码连接到某些C回调,我必须传递constchar*const*,即字符串数组。这是我的代码的简化版本:intmain(int,char**){constintcnt=10;constchar*const*names=static_cast(malloc(sizeof(char*)*cnt));//...allocatingnames[0],etc.comingsoon...the_c_function(names);free(names);return0;}所以我

c++ - 如果 C++ 类同时包含 const 引用和非 const 引用复制构造函数怎么办?

片段1:#includeusingnamespacestd;classC{public:C(){}C(constC&c){cout输出:调用的const复制构造函数片段2:#includeusingnamespacestd;classC{public:C(){}C(constC&c){cout输出:调用了非常量复制构造函数片段3:#includeusingnamespacestd;classC{public:C(){}C(constC&c){cout输出:错误:复制构造函数必须通过引用传递它的第一个参数我很困惑:对于片段2,为什么此处的非常量复制构造函数有效?为什么调用非const复制

c++ - 奇怪的运算符重载, "operator T& () const noexcept { return *_ptr; }"

我研究了一下,operator函数的格式是(returnvalue)operator[space]op(arguments){implementation}但是,在std::reference_wrapper实现中,有一个运算符重载函数声明为operatorT&()constnoexcept{return*_ptr;。这个运算符和T&operator()constnoexcept{return*_ptr;不同吗?}?.如果两者不同,那么第一个有什么用? 最佳答案 运算符T&()constnoexcept;是一个user-define