简体   繁体   English

C ++模板变量和参数包扩展

[英]C++ template variable and parameter pack expansion

In libstdc++ variant source, it defines the following template variable (taken out from the struct _Traits), 在libstdc ++变种源中,它定义了以下模板变量(取自struct _Traits),

template<typename... _Types>
  static constexpr bool _S_copy_ctor = 
    (is_copy_constructible_v<_Types> && ...);

What does the '&&' do here? '&&'在这做什么?

I tried to take out the '&&' it failed to compile, so, what's difference of the two? 我试图取出'&&'它无法编译,所以,这两者有什么区别?

  static constexpr bool _S_copy_ctor = (is_copy_constructible_v<_Types> && ...);
  static constexpr bool _S_copy_ctor = (is_copy_constructible_v<_Types>    ...);

In this very context, the && is a simple logical AND operator called fold operator . 在这个上下文中, &&是一个简单的逻辑AND运算符,称为fold运算符

It's used to unfold an expression based on typename... _Types . 它用于展开基于typename... _Types的表达式。 Example: 例:

let _Types be deduced to int, double, float , then the expression: _Types推导为int, double, float ,然后表达式:

(is_copy_constructible_v<_Types> && ...)

will be expanded ( unfolded ) to: 将展开( 展开 )到:

(is_copy_constructible_v<int> &&
 is_copy_constructible_v<double> && 
 is_copy_constructible_v<float>)

You cannot simply erase the && . 你不能简单地删除&& It has it's use case. 它有它的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM