2017-08-30 141 views
2

我使用static_for根據this question。我窩2個static_for循環,編譯g++-7 -std=c++1z main.cpp,一切工作正常嵌套`static_for`導致編譯器錯誤

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

如果確實是一個編譯器錯誤,我應該報告這個錯誤,但我不知道如何找出錯誤是否已經報告。我該怎麼辦?

+2

*「內部編譯器錯誤」*始終是編譯器的錯誤。 – Jarod42

回答

2

這是一個在GCC 8(實驗)中修復的bug,是生成的程序集的一個例子,僅供參考。請注意,由於這不是最終版本,因此它不是穩定的,因此它被表示爲實驗。發佈後,可以找到完整的更改列表here

因此,請坐下來等待,或者使用其他編譯器,例如現在的鐺4.0.0,online

+0

謝謝。不幸的是,與鐺我遇到這個問題我正在處理我的代碼中的https://stackoverflow.com/questions/44262236/clang-4-build-error-on-functional-with-c1z。 – tom

+0

這是不幸的。我有點驚訝這兩個編譯器輸出的代碼相比(非模板和constexpr)版本多少:https://godbolt.org/g/6KTW16 – Jonas

+0

是不是預期?如果您查看模板版本,則可以看到兩個代碼塊重複了九次。 – tom

相關問題