Bla*_*aws 6 c++ gcc memory-management c++11 std-function
有没有办法防止std::function
gcc为更大的函数对象动态分配内存?
我希望以下代码在没有动态分配的情况下工作:
#include <functional>
#include <iostream>
// replace operator new and delete to log allocations
void* operator new (std::size_t n) {
std::cout << "Allocating " << n << " bytes" << std::endl;
return malloc(n);
}
void operator delete(void* p) throw() {
free(p);
}
class TestPlate
{
private:
int value;
public:
int getValue(){ return value; }
void setValue(int newValue) { value = newValue; }
int doStuff(const std::function<int()>& stuff) { return stuff(); }
};
int main()
{
TestPlate testor;
testor.setValue(15);
const std::function<int()>& func = std::bind(&TestPlate::getValue, &testor);
std::cout << testor.doStuff(func) << std::endl;
testor.setValue(25);
std::cout << testor.doStuff(func) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是它分配24个字节.据我所知,这是因为指向方法的指针需要16个字节,而指向类实例的指针需要另外8个字节.这似乎要么比可用于函数对象的内部存储器大,要么是一个普通的错误.
我想知道是否有任何方法可以绕过这种类型的行为,而无需更改签名std::function
或创建大量额外的包装代码.
不幸的是,GCC function
只有指向内部存储的成员函数的指针的空间,因此绑定表达式的结果不适合.
您可以使用lambda表达式代替:
std::function<int()> f = [&]{ return testor.getValue(); };
Run Code Online (Sandbox Code Playgroud)
这只需要一个包含引用的闭包类型的空间testor
(它是指向成员的指针大小的一半,并且是绑定结果大小的三分之一),并且GCC定义了该闭包,因此它可以存储在一个std::function
.