Bug 71653 - error when trying to compile a code with template friend function in a struct
Summary: error when trying to compile a code with template friend function in a struct
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: c++-friends, cxxfriends
  Show dependency treegraph
 
Reported: 2016-06-24 17:54 UTC by Raphael Gozzo
Modified: 2024-03-26 00:03 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.8.3, 6.1.0
Last reconfirmed: 2021-08-05 00:00:00


Attachments
preprocessed file (5.90 KB, text/plain)
2016-06-24 17:54 UTC, Raphael Gozzo
Details
source file (202 bytes, text/x-csrc)
2016-06-24 17:55 UTC, Raphael Gozzo
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Raphael Gozzo 2016-06-24 17:54:41 UTC
Created attachment 38764 [details]
preprocessed file

$ g++ -v -save-temps -std=c++11 -c testcase.cpp

Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.8 --enable-ssp --disable-libssp --disable-plugin --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --enable-linker-build-id --enable-linux-futex --program-suffix=-4.8 --without-system-libunwind --with-arch-32=i586 --with-tune=generic --build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
gcc version 4.8.5 (SUSE Linux) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++11' '-c' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib64/gcc/x86_64-suse-linux/4.8/cc1plus -E -quiet -v -D_GNU_SOURCE testcase.cpp -mtune=generic -march=x86-64 -std=c++11 -fpch-preprocess -o testcase.ii
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.8
 /usr/include/c++/4.8/x86_64-suse-linux
 /usr/include/c++/4.8/backward
 /usr/lib64/gcc/x86_64-suse-linux/4.8/include
 /usr/local/include
 /usr/lib64/gcc/x86_64-suse-linux/4.8/include-fixed
 /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++11' '-c' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib64/gcc/x86_64-suse-linux/4.8/cc1plus -fpreprocessed testcase.ii -quiet -dumpbase testcase.cpp -mtune=generic -march=x86-64 -auxbase testcase -std=c++11 -version -o testcase.s
GNU C++ (SUSE Linux) version 4.8.5 (x86_64-suse-linux)
        compiled by GNU C version 4.8.5, GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++ (SUSE Linux) version 4.8.5 (x86_64-suse-linux)
        compiled by GNU C version 4.8.5, GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: ca63ef29755a145e45a587295d513e14
testcase.cpp: In function ‘int main()’:
testcase.cpp:16:9: error: no matching function for call to ‘f(S*)’
     f(&s);
         ^
testcase.cpp:16:9: note: candidate is:
testcase.cpp:5:28: note: template<class T> C<T> f(T*)
 template <typename T> C<T> f(T *);
                            ^
testcase.cpp:5:28: note:   template argument deduction/substitution failed:
testcase.cpp:3:166: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class T> using C = typename std::conditional<std::is_const< <template-parameter-1-1> >::value, typename std::add_const<typename T::InnerType>::type, typename T::InnerType>::type [with T = S]’
 template <typename T> using C = typename std::conditional<std::is_const<T>::value, typename std::add_const<typename T::InnerType>::type, typename T::InnerType>::type;
                                                                                                                                                                      ^
testcase.cpp:3:166:   recursively required by substitution of ‘template<class T> using C = typename std::conditional<std::is_const< <template-parameter-1-1> >::value, typename std::add_const<typename T::InnerType>::type, typename T::InnerType>::type [with T = S]’
testcase.cpp:3:166:   required by substitution of ‘template<class T> using C = typename std::conditional<std::is_const< <template-parameter-1-1> >::value, typename std::add_const<typename T::InnerType>::type, typename T::InnerType>::type [with T = S]’
testcase.cpp:5:28:   required by substitution of ‘template<class T> C<T> f(T*) [with T = S]’
testcase.cpp:16:9:   required from here
Comment 1 Raphael Gozzo 2016-06-24 17:55:22 UTC
Created attachment 38765 [details]
source file
Comment 2 Raphael Gozzo 2016-06-24 17:57:14 UTC
This error happens in the 6.1 version too (tested here http://gcc.godbolt.org)
Comment 3 Andrew Pinski 2021-08-05 08:47:22 UTC
Reduced testcase without includes:
template <typename T> using C = typename T::InnerType;
template <typename T> C<T> f(T &);
class S {
    using InnerType = int;
    template <typename T> friend C<T> f(T &);
};
int main()
{
    S s;
    f(s);
    return 0;
}
Comment 4 Andrew Pinski 2021-08-05 08:51:05 UTC
If I don't use an alias template in the original declaration of f it works:
template <typename T> using C = typename T::InnerType;
template <typename T> typename T::InnerType f(T &);
class S {
    using InnerType = int;
    template <typename T> friend C<T> f(T &);
};
int main()
{
    S s;
    f(s);
    return 0;
}