折叠表达式:解析器堆栈溢出

Fold Expression: parser stackoverflow

本文关键字:堆栈 栈溢出 表达式 折叠      更新时间:2023-10-16

嗨,我想,运算符使用折叠表达式,但 MSVC 一直用 C1026->程序让我烦恼。我将问题分解为最小示例:

#include <utility>
#include <iostream>
template<size_t idx>
void foo()
{
//do some stuff
}
template<typename Ts>
struct ApplySomeFun;
template<size_t... Ts >
struct ApplySomeFun<std::index_sequence<Ts...>>
{
static void execute() 
{
(void(foo<Ts>()), ...);// C1026
}

};
int main()
{   
ApplySomeFun<std::make_index_sequence<1024>>::execute();
} 

这适用于 gcc,但不适用于 msvc。所以我的问题是如何在 msvc 中构建它并保持折叠表达式的清晰度。

这是我的工作轮(感谢 max66 的提示(。

template<size_t idx>
void foo()
{
std::cout << idx << std::endl;
}
template<typename Ts>
struct ApplySomeFun;
template<size_t... Ts >
struct ApplySomeFun<std::index_sequence<Ts...>>
{
static void execute()
{
int unused[] = { 0, ((void)foo<Ts>(), 0)... };//Expander trick
(void)unused; // blocks warnings
}

};
int main()
{
ApplySomeFun<std::make_index_sequence<1024>>::execute();
}

虽然不那么好,但效果很好。