嵌套的`static_for`会导致编译器错误

时间:2017-08-30 17:15:20

标签: c++ compiler-errors g++

我根据this question使用static_for。我嵌套了两个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) 

如果它确实是一个编译器错误,我应该报告这个错误,但我不知道如何查明是否已报告该错误。我该怎么办?

1 个答案:

答案 0 :(得分:3)

这是一个已在GCC 8(实验性)中修复的错误,here是生成的程序集的一个示例,只是为了好玩。请注意,由于这不是最终版本,因此不稳定,因此将其表示为 experimental 。发布后,可以找到完整的更改列表here

所以,请等一下,或者使用其他编译器,例如clang 4.0.0,online