Effective Modern C++ Item 27:重载universal references

时间:2022-12-16 10:19:38

假设有一个接收universal references的模板函数foo,定义如下:

template<typename T>
void foo(T&& t)
{
cout << "foo(T&& t)" << endl;
}

如果想对某些类型做特殊处理,写一个重载版本的foo,比如想对float类型做特殊处理,就写一个接收float类型的foo:

void foo(float n)
{
cout << "foo(float n)" << endl;
}

这样,如果我们写下 foo(1.0) 时,理论上应该输出"foo(float n)",而实际上输出结果为"foo(T&& t)"。为什么呢?因为“Functions taking universal reference are the greediest functions in C++”,也就是说universal reference的函数能准确匹配几乎所有的类型。当我们调用foo(1.0)时,1.0被推导为double类型,如果调用foo(float n),就需要做narrow conversion,所以编译器会认为foo(T&& t)为更准确的匹配,除非我们写下foo(1.f)时,才会调用foo(float)。只有在类型完全准确匹配时,才会调用重载版本,否则编译器会始终认为universal reference版本为准确匹配。

这个问题在类继承中会更为隐晦,假设有一个名为Base的class,Base有一个接收universal reference的模板构造函数,定义如下:

class Base
{
public:
template<typename T>
explicit Base(T&& t)
{
cout << "Base(T&& t)" << endl;
} Base(const Base& b)
{
cout << "Base(const Base& b)" << endl;
} Base(Base&& b)
{
cout << "Base(Base&& b)" << endl;
} Base() = default;
};

然后Derived继承Base:

class Derived : public Base
{
public:
Derived() = default; Derived(const Derived& d)
:Base(d)
{
} Derived(Derived&& d)
:Base(std::move(d))
{
}
};

这时候,如果我们写:

Derived a;
Derived b(a);
Derived c(std::move(a));

那么输出结果始终为“Base(T&& t)”,也就是在Derived的拷贝构造和移动构造中,Base的函数调用都是Base(T&& t)。因为传给Base的类型为Derived,所以编译器始终认为universal reference为准确匹配。

由于universal reference的匹配过于"strong",一般都要避免重载,否则很容易出现匹配结果和预期不一致的情况。

如果无法避免重载呢?有两种方法:

1.使用type tags

考虑之前的foo函数,我们不重载foo函数,而是编写两个重载的fooImpl,fooImpl一个接受universal reference,一个接受float,两个函数用type tag参数来区分:

template<typename T>
void fooImpl(T&& t, std::false_type)
{
cout << "fooImpl(T&& t)" << endl;
} void fooImpl(float t, std::true_type)
{
cout << "fooImpl(float t)" << endl;
}

参数的type tag表示是否为浮点类型,那么foo就可以这么调用:

template<typename T>
void foo(T&& t)
{
fooImpl(std::forward<T>(t), std::is_floating_point<std::remove_reference_t<T>>());
}

这样有了type tag,只要参数是浮点类型,都会调用float版本的fooImpl。

2. 使用enable_if约束universal reference

如果某些情况我们不想使用universal reference版本,那么可以使用enable_if把它在重载决议的候选函数中屏蔽掉(SFINAE机制)。

对于foo函数,改写为:

template<typename T, typename = std::enable_if_t<
!std::is_floating_point<
std::remove_reference_t<T>
>::value>
>
void foo(T&& t)
{
cout << "foo(T&& t)" << endl;
} void foo(float t)
{
cout << "foo(float t)" << endl;
}

这样,如果参数为浮点类型,foo(T&& t)会被屏蔽掉,就会调用float版本的foo,这就和预期结果一致。

对于之前的Base class,思路也是一样的。用enable_if改写之前的代码:

class Base
{
public:
template<typename T, typename = std::enable_if_t<
!std::is_base_of <Base, std::decay_t<T>>::value
>>
explicit Base(T&& t)
{
cout << "Base(T&& t)" << endl;
} Base(const Base& b)
{
cout << "Base(const Base& b)" << endl;
} Base(Base&& b)
{
cout << "Base(Base&& b)" << endl;
} Base() = default;
};

这样,Derived的拷贝构造和移动构造就能正确调用到Base的函数(std::decay去掉references和cv-qualifiers)。

结论:

1. 尽量避免重载universal references模板函数。

2. 如果无法避免,使用type tags或者enable_if来编写重载函数。

Effective Modern C++ Item 27:重载universal references的更多相关文章

  1. &lbrack;Effective Modern C&plus;&plus;&rsqb; Item 1&period; Understand template type deduction - 了解模板类型推断

    条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...

  2. &lbrack;Effective Modern C&plus;&plus;&rsqb; Item 7&period; Distinguish between &lpar;&rpar; and &lbrace;&rcub; when creating objects - 辨别使用&lpar;&rpar;与&lbrace;&rcub;创建对象的差别

    条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...

  3. &lbrack;Effective Modern C&plus;&plus;&rsqb; Item 6&period; Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句

    条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...

  4. &lbrack;Effective Modern C&plus;&plus;&rsqb; Item 5&period; Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto

    条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...

  5. &lbrack;Effective Modern C&plus;&plus;&rsqb; Item 4&period; Know how to view deduced types - 知道如何看待推断出的类型

    条款四 知道如何看待推断出的类型 基础知识 有三种方式可以知道类型推断的结果: IDE编辑器 编译器诊断 运行时输出 使用typeid()以及std::type_info::name可以获取变量的类型 ...

  6. &lbrack;Effective Modern C&plus;&plus;&rsqb; Item 3&period; Understand decltype - 了解decltype

    条款三 了解decltype 基础知识 提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪. 以下是一些简单的推断类型: ; // decltype(i) -> ...

  7. &lbrack;Effective Modern C&plus;&plus;&rsqb; Item 2&period; Understand auto type deduction - 了解auto类型推断

    条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...

  8. Effective Modern C&plus;&plus; Item 37:确保std&colon;&colon;thread在销毁时是unjoinable的

    下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...

  9. Effective Modern C&plus;&plus; 42 Specific Ways to Improve Your Use of C&plus;&plus;11 and C&plus;&plus;14

    Item 1: Understand template type deduction. Item 2: Understand auto type deduction. Item 3: Understa ...

随机推荐

  1. effective OC2&period;0 52阅读笔记(七 系统框架)

    47 熟悉系统框架 总结:将代码封装为动态库,并提供接口的头文件,就是框架.平时的三方应用都用静态库(因为iOS应用程序不允许在其中包含动态库),并不是真正的框架,然而也经常视为框架.例如:NSLin ...

  2. 一次受限于操作系统进程数的OOM

    在64bit机上跑应用,结果进程刚起来就挂,就刚起来就挂..还OOM,还fork不出新进程,尼玛,这什么情况? 1. 如果是应用层面OOM,那么不应该任何命令都不被执行了,不应该OS直接crash掉. ...

  3. 分布式架构高可用架构篇&lowbar;08&lowbar;MyCat在MySQL主从复制基础上实现读写分离

    参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...

  4. 【转载】Visual Studio 2015 for Linux更好地支持Linux下的开发

    原文:Visual Studio 2015 for Linux更好地支持Linux下的开发 英文原文:Targeting Linux Made Easier in Visual Studio 2015 ...

  5. python 连接数据库-设置oracle ,mysql 中文字符问题

    import cx_Oracle import MySQLdb def conn_oracle(): cnn = cx_Oracle.connect('用户名','密码','ip:端口号/数据库') ...

  6. JS中sort&lpar;&rpar;方法原理及使用

    说明 如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序.要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以便进行比较. arra ...

  7. myeclipse和eclipse的区别和联系,以及版本间的对应关系

    Eclipse:IBM花了4千万美金来开发这个IDE(Integrated Development Environment).第一版1.0在2001年11月释出,随后逐渐受到欢迎.Eclipse已经成 ...

  8. Linux 文件内容查看工具介绍-cat&comma;less&comma;more&comma;tail&comma;head

    Linux 文件内容查看工具介绍 作者:北南南北来自:LinuxSir.Org摘要: 本文讲述几种常用文件内容的查看工具,比如cat.more.less.head.tail等,把这些工具最常用的参数. ...

  9. gradle多项目 svn依赖

    当svn有多个子项目且没有根项目的时候,用eclipse拷贝下来是容易出问题的,经常子项目之间的依赖会有问题,还是推荐用IDEA. 操作说明: 如果SVN有 A,B,C,D四个项目,A为web项目,B ...

  10. htm中的 src未指定具体路径的话 默认查找当前文件夹

    htm中的 src未指定具体路径的话 默认查找当前文件夹