3

I use static_for based on this question. I nest two static_for loops, compile with g++-7 -std=c++1z main.cpp and everything works fine

#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;
}

But when I change the main function to

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;
}

I get a compiler error

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.

My g++ version

$ 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) 

If it is really a compiler error I should probably report this bug, but I have no idea how to find out if the bug has been reported or not. What should I do?

tom
  • 1,520
  • 1
  • 12
  • 26

1 Answers1

3

This is a bug that has been fixed in GCC 8 (experimental), here is an example of the generated assembly, just for fun. Please note that since this not a final release it is not stable, hence it is denoted as experimental. When it is released, a complete list of changes can be found here.

So, sit back and wait, or use another compiler such as clang 4.0.0 for now, online.

Jonas
  • 6,915
  • 8
  • 35
  • 53
  • Thank you. Unfortunately with clang I encounter this problem https://stackoverflow.com/questions/44262236/clang-4-build-error-on-functional-with-c1z in my code I'm working on. – tom Aug 30 '17 at 17:56
  • That is unfortunate. I'm a bit amazed how much code the two compilers output, compared to this (non template nor constexpr) version: https://godbolt.org/g/6KTW16 – Jonas Aug 30 '17 at 18:04
  • Isn't it expected? If you look at the template version then you can see that two blocks of code are repeated exactly nine times. – tom Aug 30 '17 at 19:19
  • @tom I would expect the compiler to remove code "duplication" when optimizing – Jonas Aug 30 '17 at 19:21
  • @RustyX well, not in this case, the run-time will be dominated by the prints. But normally code size can be proxy for speed, though sometimes it can be miss leading. – Jonas Aug 30 '17 at 19:26
  • It's a balance between I-Cache pollution and mispredicted branches. – rustyx Aug 30 '17 at 19:38