[Bug c++/88694] constexpr isn't captured correctly in lambda

2019-01-04 Thread amosbird at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88694

--- Comment #2 from Amos Bird  ---
Currently by introducing a `auto local_i = decltype(i)();` can workaround this.



#include 
#include 
#include 

template 
constexpr void static_for_impl(Func&& f, std::integer_sequence) {
(f(std::integral_constant{}), ...);
}

template  constexpr void
static_for(Func&& f) {
static_for_impl(std::forward(f),
std::make_integer_sequence{});
}

struct Foo {
template  void print() { std::cout << x << std::endl; }
};

int main() {
Foo foo;
static_for([&](auto i) {
[&]() {
auto local_i = decltype(i)();
foo.print();
}();
});
return 0;
}

[Bug c++/88694] constexpr isn't captured correctly in lambda

2019-01-04 Thread amosbird at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88694

--- Comment #1 from Amos Bird  ---
Created attachment 45343
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45343=edit
the ii file

[Bug c++/88694] New: constexpr isn't captured correctly in lambda

2019-01-04 Thread amosbird at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88694

Bug ID: 88694
   Summary: constexpr isn't  captured correctly in lambda
   Product: gcc
   Version: 8.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: amosbird at gmail dot com
  Target Milestone: ---

Created attachment 45342
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45342=edit
source code

This might be related to

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86594

and 

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82643


Tested with:
g++ -MMD -MP -DCC_PLAYGROUND  -std=c++1z -O2 -v -save-temps
-I/home/amos/git/ccutils/ -I/usr/include -I/usr/local/include -c snippet.cpp -o
snippet.rel.o

Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --with-system-zlib
--enable-languages=c,c++ --enable-gold
Thread model: posix
gcc version 8.2.0 (GCC) 
COLLECT_GCC_OPTIONS='-MMD' '-MP' '-D' 'CC_PLAYGROUND' '-std=c++17' '-O2' '-v'
'-save-temps' '-I' '/home/amos/git/ccutils/' '-I' '/usr/include' '-I'
'/usr/local/include' '-c' '-o' 'snippet.rel.o' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /usr/local/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1plus -E -quiet -v -I
/home/amos/git/ccutils/ -I /usr/include -I /usr/local/include -MMD
snippet.rel.d -MP -MQ snippet.rel.o -D_GNU_SOURCE -D CC_PLAYGROUND snippet.cpp
-mtune=generic -march=x86-64 -std=c++17 -O2 -fpch-preprocess -o snippet.ii
ignoring nonexistent directory
"/usr/local/lib/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../x86_64-pc-linux-gnu/include"
ignoring duplicate directory "/usr/include"
  as it is a non-system directory that duplicates a system directory
ignoring duplicate directory "/usr/local/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /home/amos/git/ccutils/
 /usr/local/lib/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../include/c++/8.2.0

/usr/local/lib/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../include/c++/8.2.0/x86_64-pc-linux-gnu

/usr/local/lib/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../include/c++/8.2.0/backward
 /usr/local/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include
 /usr/local/include
 /usr/local/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-MMD' '-MP' '-D' 'CC_PLAYGROUND' '-std=c++17' '-O2' '-v'
'-save-temps' '-I' '/home/amos/git/ccutils/' '-I' '/usr/include' '-I'
'/usr/local/include' '-c' '-o' 'snippet.rel.o' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
 /usr/local/libexec/gcc/x86_64-pc-linux-gnu/8.2.0/cc1plus -fpreprocessed
snippet.ii -quiet -dumpbase snippet.cpp -mtune=generic -march=x86-64
-auxbase-strip snippet.rel.o -O2 -std=c++17 -version -o snippet.s
GNU C++17 (GCC) version 8.2.0 (x86_64-pc-linux-gnu)
compiled by GNU C version 8.2.0, GMP version 6.1.0, MPFR version 3.1.4,
MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++17 (GCC) version 8.2.0 (x86_64-pc-linux-gnu)
compiled by GNU C version 8.2.0, GMP version 6.1.0, MPFR version 3.1.4,
MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 4e924b3bd09bf840dfb30b5ba0fbc5b8
snippet.cpp: In instantiation of ‘main():: [with auto:1 =
std::integral_constant]’:
snippet.cpp:8:7:   required from ‘constexpr void static_for_impl(Func&&,
std::integer_sequence) [with T = int; T Begin = 0; Func =
main()::; T ...Is = {0, 1, 2}]’
snippet.cpp:12:30:   required from ‘constexpr void static_for(Func&&) [with T =
int; T Begin = 0; T End = 3; Func = main()::]’
snippet.cpp:21:26:   required from here
snippet.cpp:21:49: error: no matching function for call to ‘Foo::print()’
 static_for([&](auto i) { [&]() { foo.print(); }(); });
 ^~~
snippet.cpp:16:27: note: candidate: ‘template void Foo::print()’
 template  void print() { std::cout << x << std::endl; }
   ^
snippet.cpp:16:27: note:   template argument deduction/substitution failed:
snippet.cpp:21:49: error: ‘this’ is not a constant expression
 static_for([&](auto i) { [&]() { foo.print(); }(); });
 ^~~
snippet.cpp:21:49: note: in template argument for type ‘int’
snippet.cpp: In instantiation of ‘main():: [with auto:1 =
std::integral_constant]’:
snippet.cpp:8:7:   required from ‘constexpr void static_for_impl(Func&&,
std::integer_sequence) [with T = int; T Begin = 0; Func =
main()::; T ...Is = {0, 1, 2}]’
snippet.cpp:12:30:   required from ‘constexpr void static_for(Func&&) [with T =
in