草庐IT

static-memory-allocation

全部标签

c++ - 使用static_assert时如何避免没有返回表达式的警告?

我有两个库可以使用,并且为了方便起见,我在它们使用的一些类型/结构之间编写了一个转换器。templatestructunsupportedType:std::false_type{};templateFormatBgetFormat(){static_assert(unsupportedType::value,"Thisisnotsupported!");}templateFormatBgetFormat(){returnFormatB(//someparameters);}templateFormatBgetFormat(){returnFormatB(//someotherpara

c++ - 虚拟析构函数 : is it required when not dynamically allocated memory?

如果我的类不动态分配任何内存,我们是否需要虚拟析构函数?例如classA{private:inta;intb;public:A();~A();};classB:publicA{private:intc;intd;public:B();~B();};在这种情况下,我们需要将A的析构函数标记为虚拟吗? 最佳答案 问题不在于您的类是否动态分配内存。如果类的用户通过A指针分配B对象,然后将其删除:A*a=newB;deletea;在这种情况下,如果A没有虚拟析构函数,C++标准会说您的程序表现出未定义的行为。这可不是什么好事。此行为在标准的

c++ - 虚拟析构函数 : is it required when not dynamically allocated memory?

如果我的类不动态分配任何内存,我们是否需要虚拟析构函数?例如classA{private:inta;intb;public:A();~A();};classB:publicA{private:intc;intd;public:B();~B();};在这种情况下,我们需要将A的析构函数标记为虚拟吗? 最佳答案 问题不在于您的类是否动态分配内存。如果类的用户通过A指针分配B对象,然后将其删除:A*a=newB;deletea;在这种情况下,如果A没有虚拟析构函数,C++标准会说您的程序表现出未定义的行为。这可不是什么好事。此行为在标准的

c++ - 什么时候应该在非成员函数之前写关键字 'static'?

我最近在SO上看到了一些关于函数前的static关键字的信息,我想知道如何正确使用它。1)什么时候应该在非成员函数前写关键字static?2)在header中定义静态非成员函数是否危险?为什么(不)?(附带问题)3)是否可以以某种方式在头文件中定义一个类,以便它只在您首先使用它的翻译单元中可用?(我问这个的原因是因为我正在学习STL,它可能是我的谓词等(可能是仿函数)的一个很好的解决方案,因为我不喜欢定义成员以外的函数-cpp文件中的函数)(另外,我认为它在某种程度上与原始问题相关,因为根据我目前的推理,它会在函数之前做与static相同的事情)编辑看到一些答案时提出的另一个问题:4)

c++ - 什么时候应该在非成员函数之前写关键字 'static'?

我最近在SO上看到了一些关于函数前的static关键字的信息,我想知道如何正确使用它。1)什么时候应该在非成员函数前写关键字static?2)在header中定义静态非成员函数是否危险?为什么(不)?(附带问题)3)是否可以以某种方式在头文件中定义一个类,以便它只在您首先使用它的翻译单元中可用?(我问这个的原因是因为我正在学习STL,它可能是我的谓词等(可能是仿函数)的一个很好的解决方案,因为我不喜欢定义成员以外的函数-cpp文件中的函数)(另外,我认为它在某种程度上与原始问题相关,因为根据我目前的推理,它会在函数之前做与static相同的事情)编辑看到一些答案时提出的另一个问题:4)

c++ - Linux乐观malloc : will new always throw when out of memory?

我一直在阅读有关Linux内存不足的情况,手册页中的以下段落让我思考:Bydefault,Linuxfollowsanoptimisticmemoryallocationstrategy.Thismeansthatwhenmalloc()returnsnon-NULLthereisnoguaranteethatthememoryreallyisavailable.Thisisareallybadbug.Incaseitturnsoutthatthesystemisoutofmemory,oneormoreprocesseswillbekilledbytheinfamousOOMkill

c++ - Linux乐观malloc : will new always throw when out of memory?

我一直在阅读有关Linux内存不足的情况,手册页中的以下段落让我思考:Bydefault,Linuxfollowsanoptimisticmemoryallocationstrategy.Thismeansthatwhenmalloc()returnsnon-NULLthereisnoguaranteethatthememoryreallyisavailable.Thisisareallybadbug.Incaseitturnsoutthatthesystemisoutofmemory,oneormoreprocesseswillbekilledbytheinfamousOOMkill

c++ - static const std::map<string, int> vs if-elseif

我编写了一个将字符串转换为数字的函数。我看到了两种可能的写法:intconvert(conststd::stringinput){if(input=="one"){return1;}elseif(input=="two"){return2;}//etc.return0;}或者intconvert(conststd::stringinput){staticconstmaptable={{"one",1},{"two",2}//etc.}constautoresult=table.find(input);if(result==table.end()){return0;}returnresu

c++ - static const std::map<string, int> vs if-elseif

我编写了一个将字符串转换为数字的函数。我看到了两种可能的写法:intconvert(conststd::stringinput){if(input=="one"){return1;}elseif(input=="two"){return2;}//etc.return0;}或者intconvert(conststd::stringinput){staticconstmaptable={{"one",1},{"two",2}//etc.}constautoresult=table.find(input);if(result==table.end()){return0;}returnresu

c++ - `static constexpr` 在常量表达式中调用的函数是...错误?

我有以下代码:classMyClass{staticconstexprboolfoo(){returntrue;}voidbar()noexcept(foo()){}};我希望因为foo()是一个staticconstexpr函数,并且由于它是在声明bar之前定义的,所以这将是完全可以接受。但是,g++给了我以下错误:error:‘staticconstexprboolMyClass::foo()’calledinaconstantexpression这...没什么用,因为在常量表达式中调用函数的能力是constexpr的全部要点。clang++更有帮助。除了指出noexcept的参数