https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88201
Bug ID: 88201
Summary: lambda function fails to compile, when deactivated by
a constexpr and deduced return type
Product: gcc
Version: 8.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: j.tourscher at alpi dot fr
Target Milestone: ---
Created attachment 45095
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45095&action=edit
preprocessed test file
Hello,
I have a compilation error when trying to compile code using a lambda function
that is deactivated by a "if constexpr".
The code fails to compile when the lambda has a deduced return type, and is
stored inside a std::function or a C function pointer.
But when re-enabling the constexpr condition, or specifiyng a return type for
the lambda, the code compiles.
code that produces the error :
int main(int , char* [])
{
int (*f)() ;
if constexpr (false)
f = []() { return 0; };
return 0;
}
command line to compile the test :
gcc test.cpp -o test -lstdc++ -std=c++17 -Wall -Wextra --save-temps
output from gcc :
test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:30: error: invalid user-defined conversion from ‘main(int,
char**)::<lambda()>’ to ‘int (*)()’ [-fpermissive]
f = []() { return 0; };
^
test.cpp:6:16: note: candidate is: ‘constexpr main(int,
char**)::<lambda()>::operator void (*)()() const’ <near match>
f = []() { return 0; };
^
test.cpp:6:16: note: no known conversion from ‘void (*)()’ to ‘int (*)()’
comments :
- changing "constexpr (false)" to "constexpr (true)" makes the compilation to
succeed
- changing "f = []() { return 0; };" to "f = []() -> int { return 0; };" makes
the compilation to succeed
- the code compiles well with clang 6.0, or with msvc.
- using "std::function<int (void)>" instead of function pointer produces the
same error
gcc -v output :
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --with-isl --enable-libmpx
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.2.1 20181105 (Red Hat 8.2.1-5) (GCC)
Regards