c++ - 具有空参数包的一元折叠
问题描述
#include <iostream>
#include <utility>
template<std::size_t... items>
constexpr std::size_t count()
{
return std::index_sequence<items...>().size();
}
template<std::size_t... items>
constexpr std::size_t fold_mul()
{
if( count<items...>() == 0 )
{
return 1;
}
else
{
return (... * items);
}
}
int main()
{
std::cout << "Result: " << fold_mul<>() << "\n";
}
此代码预计会输出1
,但会引发错误:
<translation>:19:28: 错误:运算符* 19上的空扩展折叠 退换货品);
我的问题是:为什么这不起作用,因为 fold_expression 显然在该else
部分中。
作为参考,此实现有效:
template<typename... Args>
constexpr std::size_t fold_mul();
template<std::size_t... j>
requires (count<j...>() > 0)
constexpr std::size_t fold_mul()
{
return (j * ...);
}
template<>
constexpr std::size_t fold_mul()
{
return 1;
}
解决方案
问题是,当指定带有空扩展的折叠表达式时,(... * items)
在编译时无效;即使它不会在运行时进行评估。
您可以使用constexpr if (C++17 起); 那么当指定带有空扩展的折叠表达式时,else-part 将被丢弃。
如果值为 true,则丢弃statement-false(如果存在),否则丢弃statement-true。
template<std::size_t... items>
constexpr std::size_t count()
{
return std::index_sequence<items...>().size();
}
template<std::size_t... items>
constexpr std::size_t fold_mul()
{
if constexpr ( count<items...>() == 0 )
// ^^^^^^^^^
{
return 1;
}
else
{
return (... * items);
}
}
推荐阅读
- javascript - 来自 mixin 的 Emberjs 动态注入
- timer - 定时器触发的ADC转换不工作,STM32L4
- woocommerce - WooCommerce 使用 Country 和 Postcode 以自定义方法进行运费估算
- android - 如何子类化 Android Studio 中可用的 Javadoc Doclet?
- javascript - 如何使用cropper.js动态裁剪两个不同纵横比的图像?
- blas - 简单的 cblas gemm 代码但奇怪的结果
- java - RecyclerView onClick 给出了错误的项目
- ios - 错误 Appstore 连接:Info.plist 文件中缺少目的字符串 (NSBluetoothPeripheralUsageDescription)
- c# - HttpContext.Current.User 返回 system.security.claims.windowsprincipal 而不是 system.security.claims.ClaimPrincipal
- php - PHP变量名连接不起作用