找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2231|回复: 12

使用using关键字选择定制或通用函数

 火.. [复制链接]
  • 打卡等级:即来则安
  • 打卡总天数:28
  • 打卡月天数:7
  • 打卡总奖励:7961
  • 最近打卡:2025-12-22 17:16:30

3317

主题

285

回帖

2万

积分

管理员

积分
24106
发表于 2021-4-3 12:13:15 | 显示全部楼层 |阅读模式


C.165: Use using for customization points
C.165: 为定制点使用using关键字
Reason(原因)
To find function objects and functions defined in a separate namespace to "customize" a common function.
为了发现那些为了定制共通函数而定义于单独的命名空间内的函数对象和函数。
Example(示例)
Consider swap. It is a general (standard-library) function with a definition that will work for just about any type. However, it is desirable to define specific swap()s for specific types. For example, the general swap() will copy the elements of two vectors being swapped, whereas a good specific implementation will not copy elements at all.
考虑交换函数。它是一个一般的(标准库)可以适用于任何类型的函数。然而,也希望可以为特殊类型定义特殊的交换函数。例如,通常的交换函数会复制作为交换对象的vector的元素,然而好的特殊实现应该根本不复制元素。
namespace N {    My_type X { /* ... */ };    void swap(X&, X&);   // optimized swap for N::X    // ...}void f1(N::X& a, N::X& b){    std::swap(a, b);   // probably not what we wanted: calls std::swap()}
The std::swap() in f1() does exactly what we asked it to do: it calls the swap() in namespace std. Unfortunately, that's probably not what we wanted. How do we get N::X considered?
函数f1中的std::swap()会准确执行我们所要求的:它调用std命名空间中的swap()。不幸的是那可能不是我们想要的。怎样才能执行我们期待的N:X?
void f2(N::X& a, N::X& b){    swap(a, b);   // calls N::swap}
But that may not be what we wanted for generic code. There, we typically want the specific function if it exists and the general function if not. This is done by including the general function in the lookup for the function:
但是这样(上面的代码那样,译者注)做不是一般代码中应该有的样子。这里我么一般的想法是:如果存在特殊函数就执行它而不是一般函数。实现这种功能的方法就是将通用函数包含再函数的检索范围内。
void f3(N::X& a, N::X& b){    using std::swap;  // make std::swap available    swap(a, b);        // calls N::swap if it exists, otherwise std::swap}
Enforcement(实施建议)
Unlikely, except for known customization points, such as swap. The problem is that the unqualified and qualified lookups both have uses.
不太可能实现。除非是已知的定制点,例如swap函数。问题是符合条件和不符合条件的查找都有用。
原文链接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c165-use-using-for-customization-points

工控课堂 www.gkket.com

0

主题

590

回帖

1744

积分

高级会员

积分
1744
发表于 2021-4-3 12:13:16 | 显示全部楼层
真是被感动的痛哭流涕……
工控课堂 www.gkket.com

0

主题

94

回帖

199

积分

新手上路

积分
199
发表于 2021-4-3 12:19:41 | 显示全部楼层
无私奉献,好工控人,32个赞送给你!!
工控课堂 www.gkket.com

0

主题

102

回帖

167

积分

新手上路

积分
167
发表于 2025-11-14 17:15:52 | 显示全部楼层
学到了学到了,这波分享太实用啦!
工控课堂 www.gkket.com

0

主题

431

回帖

2681

积分

高级会员

积分
2681
发表于 2025-11-14 19:10:38 | 显示全部楼层
这逻辑绝了,分析得太到位了吧
工控课堂 www.gkket.com

0

主题

73

回帖

123

积分

新手上路

积分
123
发表于 2025-11-14 22:03:03 | 显示全部楼层
路过打卡,为优质内容疯狂打 call
工控课堂 www.gkket.com

0

主题

91

回帖

138

积分

新手上路

积分
138
发表于 2025-11-14 23:08:02 | 显示全部楼层
蹲个后续,楼主记得更新呀,在线等挺急的~
工控课堂 www.gkket.com

0

主题

51

回帖

91

积分

新手上路

积分
91
发表于 2025-11-15 14:02:59 | 显示全部楼层
理性围观,感觉大家说的都有道理~
工控课堂 www.gkket.com

0

主题

90

回帖

144

积分

新手上路

积分
144
发表于 2025-11-15 14:12:48 | 显示全部楼层
画面感太强了,仿佛身临其境!
工控课堂 www.gkket.com
  • 打卡等级:即来则安
  • 打卡总天数:29
  • 打卡月天数:1
  • 打卡总奖励:7791
  • 最近打卡:2025-12-13 17:25:16

2540

主题

1355

回帖

2万

积分

管理员

积分
21304
发表于 2025-11-15 14:13:01 | 显示全部楼层
水个经验,支持楼主,加油呀
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-23 01:57 , Processed in 0.104544 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表