gpt4 book ai didi

c++ - 折叠表达式 : parser stackoverflow

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:19 25 4
gpt4 key购买 nike

您好,我想使用折叠表达式 , 运算符,但 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();
}

不是很好,但它有效。

关于c++ - 折叠表达式 : parser stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50300539/

25 4 0
文章推荐: c++ - 获取模板参数的成员变量值列表
文章推荐: java - 为什么子列表不适用于 List