是否有std::函数的独立实现
Is there a standalone implementation of std::function?
我正在开发一个嵌入式系统,所以代码大小是个问题。使用标准库使我的二进制大小增加了大约60k,从40k增加到100k。我想使用std::函数,但我无法为60k证明它的合理性。有没有我可以使用的独立实现,或者类似的实现?我正在使用它在c++11中使用绑定变量的成员函数中隐式地强制转换lambda。
这里是类似std::函数的类模板的简单实现,不包含任何头。您可以根据自己的意愿自定义行为(如移动/转发、空呼叫响应等):
live_demo
// Scroll down for example of usage
namespace bicycle
{
template<typename Result,typename ...Args>
struct abstract_function
{
virtual Result operator()(Args... args)=0;
virtual abstract_function *clone() const =0;
virtual ~abstract_function() = default;
};
template<typename Func,typename Result,typename ...Args>
class concrete_function: public abstract_function<Result,Args...>
{
Func f;
public:
concrete_function(const Func &x)
: f(x)
{}
Result operator()(Args... args) override
{
return f(args...);
}
concrete_function *clone() const override
{
return new concrete_function{f};
}
};
template<typename Func>
struct func_filter
{
typedef Func type;
};
template<typename Result,typename ...Args>
struct func_filter<Result(Args...)>
{
typedef Result (*type)(Args...);
};
template<typename signature>
class function;
template<typename Result,typename ...Args>
class function<Result(Args...)>
{
abstract_function<Result,Args...> *f;
public:
function()
: f(nullptr)
{}
template<typename Func> function(const Func &x)
: f(new concrete_function<typename func_filter<Func>::type,Result,Args...>(x))
{}
function(const function &rhs)
: f(rhs.f ? rhs.f->clone() : nullptr)
{}
function &operator=(const function &rhs)
{
if( (&rhs != this ) && (rhs.f) )
{
auto *temp = rhs.f->clone();
delete f;
f = temp;
}
return *this;
}
template<typename Func> function &operator=(const Func &x)
{
auto *temp = new concrete_function<typename func_filter<Func>::type,Result,Args...>(x);
delete f;
f = temp;
return *this;
}
Result operator()(Args... args)
{
if(f)
return (*f)(args...);
else
return Result{};
}
~function()
{
delete f;
}
};
}
// ___________________[ Example of usage ]___________________ //
int func1(double)
{
return 1;
}
struct Functor2
{
int operator()(double)
{
return 2;
}
};
double func3(bool,int)
{
return 3.0;
}
struct Functor4
{
double operator()(bool,int)
{
return 4.0;
}
};
int main()
{
int res = 10;
{
bicycle::function<int(double)> f{func1};
res -= f(1.0);
f = Functor2{};
res -= f(2.0);
}
{
bicycle::function<double(bool,int)> f1;
f1 = func3;
bicycle::function<double(bool,int)> f2{f1};
res -= f2(true,1);
f1 = Functor4{};
f2 = f1;
res -= f2(false,2);
}
return res;
}
60k来自编译器添加的异常处理,因为std::函数需要异常。std::function只抛出一个异常"bad_function_call"。所以我删除了引发异常的代码,现在如果调用一个空函数,它就会出错,我为自己节省了60k。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 独立读取-修改-写入顺序
- 实现一个在集合上迭代的模板函数
- 独立于实现的浮点/整数转换
- 在C++中将内部类实现为具有名称空间的独立类有什么好处
- 实现 realloc 的独立库
- 如何实现独立于平台的异步文件写入
- 我如何以可选速度实现独立的框架动作
- 作用域在两个类实现之间不完全独立
- 使用带有gsoap代理类的gsoap独立服务器实现http-get
- 基于套接字的通信独立于用于实现它的编程语言.怎么可能
- 是否有办法实现多个独立的日志系统与Boost.Log
- 基于独立的有状态的Reader和Writer基实现ReaderWriter类
- 是否有std::函数的独立实现