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

            Bug ID: 102884
           Summary: Incorrect compile error with id-expression in requires
                    clause before member initializer
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: galston at stellarscience dot com
  Target Milestone: ---

Created attachment 51645
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51645&action=edit
.ii file produced by g++ command

This compiles in MSVC and clang, but gives a compile error in gcc:

constexpr bool kExpression{ true };

struct Foo {

  template< typename T >
  Foo(T ) requires kExpression : x{0} {}

  int x;
};

int main() {
  [[maybe_unused]] Foo foo{1};
}

It will compile if parentheses are put around kExpression. Since kExpression is
an id-expression, and an id-expression is a primary expression, I think this
should be allowed as is in a requires clause (see Requires clause section here
https://en.cppreference.com/w/cpp/language/constraints). It appears to be the
member initializer that it is choking on; it will compile if the ": x{0}" part
is removed.

Thank you!

Output from gcc:

> g++ -v -save-temps -Wall -Werror -std=c++20 -c requires-clause-bug.cpp
Using built-in specs.
COLLECT_GCC=g++
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,d,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=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install
--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
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Werror' '-std=c++20' '-c'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/11/cc1plus -E -quiet -v -D_GNU_SOURCE
requires-clause-bug.cpp -mtune=generic -march=x86-64 -std=c++20 -Wall -Werror
-fpch-preprocess -o requires-clause-bug.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/11/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/11/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11

/usr/lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/11/../../../../include/c++/11/backward
 /usr/lib/gcc/x86_64-redhat-linux/11/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-Werror' '-std=c++20' '-c'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/11/cc1plus -fpreprocessed
requires-clause-bug.ii -quiet -dumpbase requires-clause-bug.cpp -dumpbase-ext
.cpp -mtune=generic -march=x86-64 -Wall -Werror -std=c++20 -version -o
requires-clause-bug.s
GNU C++20 (GCC) version 11.2.1 20210728 (Red Hat 11.2.1-1)
(x86_64-redhat-linux)
        compiled by GNU C version 11.2.1 20210728 (Red Hat 11.2.1-1), GMP
version 6.2.0, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version
isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++20 (GCC) version 11.2.1 20210728 (Red Hat 11.2.1-1)
(x86_64-redhat-linux)
        compiled by GNU C version 11.2.1 20210728 (Red Hat 11.2.1-1), GMP
version 6.2.0, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version
isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: ee1a2f208dd48c6a5d24bdcd31126e66
requires-clause-bug.cpp:7:32: error: found ‘:’ in nested-name-specifier,
expected ‘::’
    7 |   Foo(T ) requires kExpression : x{0} {}
      |                                ^
      |                                ::
requires-clause-bug.cpp:7:20: error: ‘kExpression’ is not a class, namespace,
or enumeration
    7 |   Foo(T ) requires kExpression : x{0} {}
      |                    ^~~~~~~~~~~
requires-clause-bug.cpp:7:39: error: expected unqualified-id before ‘{’ token
    7 |   Foo(T ) requires kExpression : x{0} {}
      |                                       ^
requires-clause-bug.cpp: In constructor ‘Foo::Foo(T) requires 
<erroneous-expression>’:
requires-clause-bug.cpp:7:37: error: expected ‘;’ before ‘}’ token
    7 |   Foo(T ) requires kExpression : x{0} {}
      |                                     ^
requires-clause-bug.cpp: In function ‘int main()’:
requires-clause-bug.cpp:13:29: error: no matching function for call to
‘Foo::Foo(<brace-enclosed initializer list>)’
   13 |   [[maybe_unused]] Foo foo{1};
      |                             ^
requires-clause-bug.cpp:7:3: note: candidate: ‘template<class T> Foo::Foo(T)
requires  <erroneous-expression>’
    7 |   Foo(T ) requires kExpression : x{0} {}
      |   ^~~
requires-clause-bug.cpp:7:3: note:   template argument deduction/substitution
failed:
requires-clause-bug.cpp:7:3: note: constraints not satisfied
requires-clause-bug.cpp:3:8: note: candidate: ‘constexpr Foo::Foo(const Foo&)’
    3 | struct Foo {
      |        ^~~
requires-clause-bug.cpp:3:8: note:   no known conversion for argument 1 from
‘int’ to ‘const Foo&’
requires-clause-bug.cpp:3:8: note: candidate: ‘constexpr Foo::Foo(Foo&&)’
requires-clause-bug.cpp:3:8: note:   no known conversion for argument 1 from
‘int’ to ‘Foo&&’
  • [Bug c++/102884] New: Incor... galston at stellarscience dot com via Gcc-bugs

Reply via email to