public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Vishal Subramanyam <me@vishalsubramanyam.ml>
To: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Not able to declare a template friend function inside a template class
Date: Sun, 13 Mar 2022 15:44:47 +0000	[thread overview]
Message-ID: <MA1PR0101MB1559E4D9F4C580E7DE13C640B50E9@MA1PR0101MB1559.INDPRD01.PROD.OUTLOOK.COM> (raw)

Hey,
I'm trying to declare a function template as a friend for a class which is already a template.
So the situation is something like,
Fraction.h:

    template<typename T>
    class Fraction{
        ...
        friend Fraction<T> operator + (Fraction<T> const &a, Fraction<T> const &b);
    }


Fraction.cpp:

    template<typename T>
    Fraction<T> operator+(Fraction<T> const &a, Fraction<T> const &b) {
        Fraction<T> frac(a.num * b.den + b.num * a.den, a.den * b.den);
        return frac;
    }


But I'm getting the following warning:

    warning: friend declaration ‘Fraction<T> operator+(const Fraction<T>&, const Fraction<T>&)’ declares a non-template function [-Wnon-template-friend]


Now, if I try to fix this by declaring my friend function as a template inside the class, following the example given in https://en.cppreference.com/w/cpp/language/friend 
(Section: Template Friend Operators, second example), my code would then look like:
Fraction.h

    template<typename T>
    class Fraction{
        ...
        friend Fraction<T> operator + <>(Fraction<T> const &a, Fraction<T> const &b);
    }


I'm getting three related errors that say:

    error: declaration of ‘operator+’ as non-function
    error: expected ‘;’ at end of member declaration
    error: expected unqualified-id before ‘<’ token

I'm not sure what the issue is considering I'm following the exact syntax given in cppreference.com. Is this a compiler issue? How do I resolve this?

Thanks,
Vishal Subramanyam (20CS10081)


             reply	other threads:[~2022-03-13 15:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-13 15:44 Vishal Subramanyam [this message]
2022-03-13 16:48 ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MA1PR0101MB1559E4D9F4C580E7DE13C640B50E9@MA1PR0101MB1559.INDPRD01.PROD.OUTLOOK.COM \
    --to=me@vishalsubramanyam.ml \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).