template<class = enable_if_t<...>> 有什么作用?

Cod*_*y W 8 c++ stl sfinae variadic-templates c++14

我一直在阅读 STL 文件以学习更好的方法来格式化我的代码,并学习提高效率的技巧。我一直在阅读线程文件,但我无法弄清楚某些代码的作用。

template<class _Fn,
    class... _Args,
    class = enable_if_t<!is_same<decay_t<_Fn>, thread>::value>>
    explicit thread(_Fn&& _Fx, _Args&&... _Ax)
    {   // construct with _Fx(_Ax...)
    ...
    }
Run Code Online (Sandbox Code Playgroud)

std::enable_if_t

template<bool _Test,
    class _Ty = void>
    using enable_if_t = typename enable_if<_Test, _Ty>::type;

template<class _Ty>
    struct enable_if<true, _Ty>
    {   // type is _Ty for _Test
    using type = _Ty;
    };
Run Code Online (Sandbox Code Playgroud)

该代码在线程和 str1common STL 中均受版权保护。

我唯一的问题是class = enable_if_t<...>做什么?

max*_*x66 6

寻找 SFINAE:“替换失败不是错误”。

查看std::enable_if( std::enable_if_tis only a helper using, C++14 引入的可能实现,以type更简单的方式访问)

template<bool B, class T = void>
struct enable_if {};

template<class T>
struct enable_if<true, T> { typedef T type; };
Run Code Online (Sandbox Code Playgroud)

如此iff(当且仅当)模板布尔值(第一个模板参数)是truestd::enable_if<...>::type被定义(使用第二个模板参数中的类型;void如果未表达)。

为了简单起见,在你的例子中你有

template<class _Fn,
    class... _Args,
    class = enable_if_t<!is_same<decay_t<_Fn>, thread>::value>>
    explicit thread(_Fn&& _Fx, _Args&&... _Ax)
    {   // construct with _Fx(_Ax...)
    ...
    }
Run Code Online (Sandbox Code Playgroud)

enable_if_ttypename std::enable_if<...>::type)如果第一个值 ( !std::is_same<typename std::decay<_Fn>::type, thread>::value) 是true.

那是:

  • 如果!std::is_same<typename std::decay<_Fn>::type, thread>::valuetrueclass = enable_if_t<!is_same<decay_t<_Fn>, thread>::value>>则执行替换并实现函数

  • 如果!std::is_same<typename std::decay<_Fn>::type, thread>::valuefalse,则替换class = enable_if_t<!is_same<decay_t<_Fn>, thread>::value>>失败,函数未实现,但这不是错误(SFINAE)。

为什么语言允许这样做?

因为,例如,您可以实现该功能的两个版本

template<class _Fn,
    class... _Args, //  vvvv true case
    class = enable_if_t<true == is_same<decay_t<_Fn>, thread>::value>>
    explicit thread(_Fn&& _Fx, _Args&&... _Ax)
    { /* do something */ }

template<class _Fn,
    class... _Args, //  vvvvv false case
    class = enable_if_t<false == is_same<decay_t<_Fn>, thread>::value>>
    explicit thread(_Fn&& _Fx, _Args&&... _Ax)
    { /* do something else */ }
Run Code Online (Sandbox Code Playgroud)

建议:搜索SFINAE并研究它,因为它是现代C++的重要组成部分。