小编Ole*_*ksa的帖子

基于条件的模板重载

使用类型特征,我可以执行以下操作:

template<typename Rect> Rect& move(Rect& rc, size_type<Rect> delta)
{
    rc.left += delta.width;
    rc.right += delta.width;
    rc.top += delta.height;
    rc.bottom += delta.height;
    return rc;
}
template<typename Rect> Rect& move(Rect& rc, point_type<Rect> to)
{
    int w = w(rc);
    int h = h(rc);
    rc.left = to.x;
    rc.top = to.y;
    rc.right = rc.left + w;
    rc.bottom = rc.top + h;
    return rc;
}
Run Code Online (Sandbox Code Playgroud)

但是如何在不更改函数名称的情况下允许传递任何大小和点类型?显然我不能这样做:

template<typename Rect, typename Size> Rect& move(Rect& rc, Size delta);
template<typename Rect, typename Point> Rect& move(Rect& rc, Point to); …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading metaprogramming template-meta-programming

1
推荐指数
1
解决办法
67
查看次数