From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id 24AEB3858C27 for ; Sun, 13 Mar 2022 16:48:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 24AEB3858C27 Received: by mail-wm1-x334.google.com with SMTP id 19so7985492wmy.3 for ; Sun, 13 Mar 2022 09:48:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pMU9fHRer1H0KjiFrTxYTlfRymxvz9XMQTH6EZ1nJXs=; b=g/jvqi2EaVjXJhKjaSANv6sYUN3dKJld1T2wlAyMgD1toz0XokzAndqmG2frgJ8RKy 6vQ93OWHUbGgrBHM//yKeaPESfeykE2vpV3wEwSdQELENTglJ8oXRsbKfciCKYuTpg4W fUN2UXgq1pvV1bEAagUjLA5Evd3QbQi8o6VupO4g1YyqzHBPVBuBIcPhsjE9hCNhk5pg h+V6JavTT2xvKaPbbFgpSMgBoK0QVq0vx1tAThf+HfW4LpVg5yjYv9n4esdQ9cfX7cvd +a4bxC/tyzfGVDKtC9AB+evXxwi+bAOylvpvvNg33IXgAT/DTz8723geWVr4e16dKUrg 7Enw== X-Gm-Message-State: AOAM533Uu+d8osXAlDDYjuTW4jyK/QX+4NufeTUp71ii7Z5O+tew1tXI kVfwVb6JFfsW4GCyJDVnaPaHtRl7547rykznJ14= X-Google-Smtp-Source: ABdhPJwJZ8Rllsli5H3tm1IWs0VkrOIXDFg5lfw/rVh1/NeZ7e0d0yZ9d0siG1LuayyEI9xFVU9Ta4bxhcC12pE4qMY= X-Received: by 2002:a05:600c:354f:b0:38a:29e:8540 with SMTP id i15-20020a05600c354f00b0038a029e8540mr4441974wmq.20.1647190118964; Sun, 13 Mar 2022 09:48:38 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Sun, 13 Mar 2022 16:48:27 +0000 Message-ID: Subject: Re: Not able to declare a template friend function inside a template class To: Vishal Subramanyam Cc: gcc-help X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Mar 2022 16:48:42 -0000 On Sun, 13 Mar 2022, 15:45 Vishal Subramanyam, wrote: > Hey, > I'm trying to declare a function template as a friend for a class which i= s > already a template. > So the situation is something like, > Fraction.h: > > template > class Fraction{ > ... > friend Fraction operator + (Fraction const &a, Fraction > const &b); > You don't need to say here, just call the class Fraction } > > > Fraction.cpp: > > template > Fraction operator+(Fraction const &a, Fraction const &b) { > Fraction 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 =E2=80=98Fraction operator+(const Frac= tion&, > const Fraction&)=E2=80=99 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 > class Fraction{ > ... > friend Fraction operator + <>(Fraction const &a, Fraction > const &b); > } > > > I'm getting three related errors that say: > > error: declaration of =E2=80=98operator+=E2=80=99 as non-function > error: expected =E2=80=98;=E2=80=99 at end of member declaration > error: expected unqualified-id before =E2=80=98<=E2=80=99 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? No, you are using the syntax for an explicit specialization of an existing function template, which is what cppreference shows. But your example has no previous declaration that can be specialized. How do I resolve this? > 1) Use the first syntax (without the redundant ) and ignore the warning or disable it with -Wno-non-template-friend or, 2) declare a function template which you specialize in the class: template class Fraction; template Fraction operator +(Fraction const &a, Fraction const &b); template class Fraction{ ... friend Fraction operator + <>(Fraction const &a, Fraction const &b); } or, 3) define it as a template: template class Fraction{ ... template friend Fraction operator + (Fraction const &a, Fraction const &b); }; > Thanks, > Vishal Subramanyam (20CS10081) > >