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