Date: Wed, 4 May 2022 20:34:58 -0400
On Wed, May 4, 2022 at 4:47 PM Vladimir Grigoriev via Std-Discussion <
std-discussion_at_[hidden]> wrote:
> The following program
>
> namespace S
> {
>
It looks like the "namespace S\n{" before this line is a typo. Is that so?
I removed them, and the rest of the program compiles fine in GCC and
Clang. https://godbolt.org/z/fYd8fhrjb
> #include <iostream>
>
> namespace S
> {
>
> template <unsigned N>
> class X
> {
> public:
> int i = N;
>
> friend X operator+( const X &a, const X &b ) noexcept
> {
> return { .i = a.i + b.i };
> }
> };
>
> inline X<256> operator+( const X<256> &a, const X<256> &b ) noexcept;
>
> }
>
> int main()
> {
> S::X<256>( *ptr )( const S::X<256>&a, const S::X<256>&b ) noexcept
> = &::S::operator+;
>
> std::cout << ptr( S::X<256>(), S::X<256>() ).i << '\n';
> }
>
> compiles using MS VC++ (2019) but does not compile using gcc or clang.
>
> I suspect that the problem is that the friend function is a templated
> entity that is not instantiated when the class granting friendship is
> instantiated.
>
> On the other hand, this behavior contradicts the behavior when the class
> is not a template class. Should the friend function be instantiated in such
> a case?.
>
> With best regards
> (Vlad from Moscow)
> You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or
> http://ru.stackoverflow.com
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>
std-discussion_at_[hidden]> wrote:
> The following program
>
> namespace S
> {
>
It looks like the "namespace S\n{" before this line is a typo. Is that so?
I removed them, and the rest of the program compiles fine in GCC and
Clang. https://godbolt.org/z/fYd8fhrjb
> #include <iostream>
>
> namespace S
> {
>
> template <unsigned N>
> class X
> {
> public:
> int i = N;
>
> friend X operator+( const X &a, const X &b ) noexcept
> {
> return { .i = a.i + b.i };
> }
> };
>
> inline X<256> operator+( const X<256> &a, const X<256> &b ) noexcept;
>
> }
>
> int main()
> {
> S::X<256>( *ptr )( const S::X<256>&a, const S::X<256>&b ) noexcept
> = &::S::operator+;
>
> std::cout << ptr( S::X<256>(), S::X<256>() ).i << '\n';
> }
>
> compiles using MS VC++ (2019) but does not compile using gcc or clang.
>
> I suspect that the problem is that the friend function is a templated
> entity that is not instantiated when the class granting friendship is
> instantiated.
>
> On the other hand, this behavior contradicts the behavior when the class
> is not a template class. Should the friend function be instantiated in such
> a case?.
>
> With best regards
> (Vlad from Moscow)
> You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or
> http://ru.stackoverflow.com
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>
-- *Brian Bi*
Received on 2022-05-05 00:35:12