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

标签 c++ templates visual-c++ variadic-templates c++17

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

相关文章:

c++ - 宽字符字符串文字是否以 UTF-16LE 编码?

c++ - 为什么这个正则表达式在 STL 世界中是错误的?

php - yii 中的多模型形式

c++ - g++ : error: libgomp. 规范:没有那个文件或目录

c++ - 冲突声明(外部基础与派生)

c++ - 安装多个版本时使用特定的 protobuf 版本

c++ - 方法定义期间返回类型中所需的模板参数

c++ - 如何使用模板别名删除一个不必要的参数

c++ - 无法编译 C++ 代码,因为无法访问 ifstream

c++ - 计算光线追踪器光线 - vector 会聚到相同的输出