c++ - 嵌套 `static_for` 导致编译器错误

标签 c++ compiler-errors g++

我使用 static_for 基于 this question .我嵌套了两个 static_for 循环,用 g++-7 -std=c++1z main.cpp 编译,一切正常

#include <iostream>

template <int First, int Last>
struct static_for
{
  template <typename Lambda>
  static inline constexpr void apply(Lambda const& f)
  {
    if constexpr (First < Last)
      {
    f(std::integral_constant<int, First>{});
    static_for<First + 1, Last>::apply(f);
      }
  }
};

template<int i, int j>
void bar(){
  std::cout << i << " " << j << std::endl;
}

int main(){

  static_for<0,3>
    ::apply([&](auto ii){
          static_for<0,3>
        ::apply([&](auto jj){
              bar<ii.value,jj.value>();
            });
        });

  return 0;
}

但是当我将 main 函数更改为

int main(){

  static_for<0,3>
    ::apply([&](auto ii){
          constexpr int i = ii.value;
          static_for<0,3>
        ::apply([&](auto jj){
              constexpr int j = jj.value;
              bar<i,j>();
            });
        });

  return 0;
}

编译错误

compiler_error.cpp: In instantiation of ‘main()::<lambda(auto:1)>::<lambda(auto:2)> [with auto:2 = std::integral_constant<int, 0>; auto:1 = std::integral_constant<int, 0>]’:
compiler_error.cpp:11:3:   required from ‘static constexpr void static_for<First, Last>::apply(const Lambda&) [with Lambda = main()::<lambda(auto:1)> [with auto:1 = std::integral_constant<int, 0>]::<lambda(auto:2)>; int First = 0; int Last = 3]’
compiler_error.cpp:28:10:   required from ‘main()::<lambda(auto:1)> [with auto:1 = std::integral_constant<int, 0>]’
compiler_error.cpp:11:3:   required from ‘static constexpr void static_for<First, Last>::apply(const Lambda&) [with Lambda = main()::<lambda(auto:1)>; int First = 0; int Last = 3]’
compiler_error.cpp:32:7:   required from here
compiler_error.cpp:26:29: internal compiler error: in tsubst_copy, at cp/pt.c:14539
        constexpr int i = ii.value;
                          ~~~^~~~~
0x5eb5c2 tsubst_copy
    ../../src/gcc/cp/pt.c:14539
0x5ef666 tsubst_copy
    ../../src/gcc/cp/pt.c:14515
0x5ef666 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
    ../../src/gcc/cp/pt.c:17831
0x5ef549 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
    ../../src/gcc/cp/pt.c:17611
0x5e83c7 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:16550
0x5e95f1 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:14477
0x5e95f1 tsubst_init
    ../../src/gcc/cp/pt.c:14483
0x5eb558 tsubst_copy
    ../../src/gcc/cp/pt.c:14681
0x5ef666 tsubst_copy
    ../../src/gcc/cp/pt.c:14515
0x5ef666 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
    ../../src/gcc/cp/pt.c:17831
0x5e83c7 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:16550
0x5edd52 tsubst_template_args
    ../../src/gcc/cp/pt.c:11779
0x5eea61 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
    ../../src/gcc/cp/pt.c:16736
0x5ef086 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
    ../../src/gcc/cp/pt.c:17232
0x5e83c7 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:16550
0x5e8225 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:15815
0x5e8275 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:15801
0x5e810b tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:16027
0x5e810b tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
    ../../src/gcc/cp/pt.c:16027
0x5e68a2 instantiate_decl(tree_node*, bool, bool)
    ../../src/gcc/cp/pt.c:22986
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.

我的 g++ 版本

$ g++-7 -v
Using built-in specs.
COLLECT_GCC=/usr/bin/g++-7
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.2.0-1ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.2.0 (Ubuntu 7.2.0-1ubuntu1~16.04) 

如果这真的是一个编译器错误,我可能应该报告这个错误,但我不知道如何查明这个错误是否已被报告。我该怎么办?

最佳答案

这是 GCC 8(实验性)中已修复的错误,here是生成的程序集的示例,仅供娱乐。请注意,由于这不是最终版本,它不稳定,因此它被标记为实验。发布后,可以找到完整的更改列表 here .

所以,坐等,或者暂时使用其他编译器,例如 clang 4.0.0,online .

关于c++ - 嵌套 `static_for` 导致编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45966247/

相关文章:

c++ - 具有平凡项(零乘积项)的 g++ 优化算术表达式

ubuntu - 如何从外部文件读取gcc输入

eclipse - 未找到 CLOCK_MONOTONIC

c++ - OpenCV - 缺少 DLL,但不是吗?

c++ - 运算符重载以连接字符串

c++ - 删除和修改 Boost MultiIndex 容器中的元素

c# - 输出图像字符串C++到C#父程序(不保存到硬盘)

c++ - StdAfx + 头文件 - MFC 应用程序中的包含顺序

compiler-errors - 空未声明的标识符错误

java - 静态抽象内部类访问外部类私有(private)字段java时编译器错误