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

            Bug ID: 93807
           Summary: -std=c++2a allows to omit out-of-class declaration in
                    template class
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bobogu at atlas dot cz
  Target Milestone: ---

gcc -v:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-pkgversion='Arch Linux 9.2.1+20200130-2'
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-shared
--enable-threads=posix --with-system-zlib --with-isl --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch
--disable-libssp --enable-gnu-unique-object --enable-linker-build-id
--enable-lto --enable-plugin --enable-install-libiberty
--with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib
--disable-werror --enable-checking=release --enable-default-pie
--enable-default-ssp --enable-cet=auto gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
gcc version 9.2.1 20200130 (Arch Linux 9.2.1+20200130-2)

Command line:
gcc -lstdc++ -std=c++2a main.cpp

With -std=c++2a, gcc allows me to omit out-of-class declaration of a friend
member function in a template class:

    // template <typename T>
    // class Foo;
    // template <typename T>
    // constexpr bool operator==(T lhs, const Foo<T>& rhs);

    template <typename T>
    class Foo {
    public:
        constexpr Foo(T k) : mK(k) {}

        constexpr friend bool operator==<T>(T lhs, const Foo& rhs);
    private:
        T mK;
    };

    template <typename T>
    constexpr bool operator==(T lhs, const Foo<T>& rhs) {
        return lhs == rhs.mK;
    }

    int main() {
        return 1 == Foo<int>(1) ? 0 : 1;
    }

This code compiles just fine and the binary returns 0, although I didn't find
anything in the C++20 standard that would allow us to omit the out-of-class
declaration. I would expect this code to work only if the first 4 lines were
uncommented.

The same behaviour was also reproduced on a gcc 9.1.0.

For reference, clang doesn't accept this code.

Reply via email to