Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:49:50+00:00 2026-06-14T20:49:50+00:00

While answering to a different question on SO, I came across a somewhat suspicious

  • 0

While answering to a different question on SO, I came across a somewhat suspicious compiler error with gcc. The offending snippet is

template <class T> class A;
template <class T, class U>
void operator*(A<T>, A<U>);

template <class T>
class A {
    friend void ::operator*(A<T>, A<T>);
...

whose last line gives the famous warning

friend declaration ‘void operator*(A<T>, A<T>)‘ declares a
non-template function

leading to hard errors later. The full code can be found here.

Now, the problem is I don’t think the behavior is appropriate. The standard in [temp.friend]/1 says:

For a friend function declaration that is not a template declaration:

— if the name of the friend is a qualified or unqualified template-id, the friend declaration refers to a specialization of a function template, otherwise

— if the name of the friend is a qualified-id and a matching nontemplate function is found in the specified
class or namespace, the friend declaration refers to that function, otherwise,

— if the name of the friend is a qualified-id and a matching specialization of a template function is found
in the specified class or namespace, the friend declaration refers to that function specialization, otherwise,

this is C++03; C++11 contains similar clause


A specialization of a template is defined by [temp.spec]/4:

… A specialization is a class, function, or class member that is either
instantiated or explicitly specialized (14.7.3).

and [temp.fct.spec]/1:

A function instantiated from a function template is called a function template specialization; so is an
explicit specialization of a function template. Template arguments can either be explicitly specified …

[temp.arg.explicit]/2 says this about specifying a template argument list for a function specification:

A template argument list may be specified when referring to a specialization of a function template

…

— in a friend declaration.

Trailing template arguments that can be deduced (14.8.2) may be omitted from the list of explicit template-arguments. If all of the template arguments can be deduced, they may all be omitted; in this case, the
empty template argument list <> itself may also be omitted
.

So, by [temp.fct.spec]/1, ::operator*<T,T>(A<T>, A<T>) is a function template specialization; and since the template parameters can be deduced, it can be referred to as ::operator*(A<T>, A<T>). So I conclude the qualified-id in the friend declaration denotes a function template specialization.


I think that the emphasized condition is fulfilled; therefore, the friend declaration should befriend the class with the operator template (implicit) specialization. However, gcc thinks otherwise and goes on to the fourth bullet which, only concerns friends designated by unqualified-ids, even though the friend is actually named by a qualified-id.

Is my interpretation correct or is gcc right in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:49:51+00:00Added an answer on June 14, 2026 at 8:49 pm

    I believe gcc is correct.

    First the current wording:

    if the name of the friend is a qualified-id and a matching function
    template is found in the specified class or namespace, the friend
    declaration refers to the deduced specialization of that function
    template
    (14.8.2.6), otherwise

    From [14.8.2.6 Deducing template arguments from a function declaration]:

    1 In a declaration whose declarator-id refers to a specialization of a
    function template, template argument deduction is performed to
    identify the specialization to which the declaration refers.

    Specifically, this is done for explicit instantiations (14.7.2),
    explicit specializations (14.7.3), and certain friend declarations
    (14.5.4). This is also done to determine whether a deallocation
    function template specialization matches a placement operator new
    (3.7.4.2, 5.3.4). In all these cases, P is the type of the function
    template being considered as a potential match and A is either the
    function type from the declaration or the type of the deallocation
    function that would match the placement operator new as described in
    5.3.4. The deduction is done as described in 14.8.2.5.

    2 If, for the set of function templates so considered, there is either no match or
    more than one match after partial ordering has been considered
    (14.5.6.2), deduction fails and, in the declaration cases, the program
    is ill-formed.

    In your case, template argument deduction is not performed because the declarator-id does not refer to a specialization. I think the important part is whose declarator-id refers to a specialization as the condition for this to happen. Simply put, you need the <> for the first sentence in 14.8.2.6p1 to happen (if I am reading this correctly).

    UPDATE
    Let’s break down what a declarator-id is for this situation:

    qualified-id:
    nested-name-specifier templateopt unqualified-id
    :: identifier
    :: operator-function-id
    :: literal-operator-id
    :: template-id
    

    As seen from the above grammar, void ::operator*(A<T>, A<T>) is a :: operator-function-id and not a :: template-id. What this means is the syntax can never declare a template function (as mentioned in the error message). For it to be a template-id you have to use operator-function-id < template-argument-listopt> syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While answering this question I came across an interesting difference in the overload resolution
I came across this interesting situation while answering this question . Try this piece
While answering one of the question, I came across this interesting situation. If I
While answering a different question , I could not find a reason for why
While answering a question i came upon this StringCollection sc = new StringCollection(); sc.Add(Foo);
Kicking around some small structures while answering this post , I came across the
While answering this question I noticed that I have never come across any property
First, some context: while answering questions on SO, I came across a post wherein
While answering to a question about that here: https://stackoverflow.com/a/9872630/82609 I tried to do the
While answering this question, I got these confusing results: double d = 0.49999999999999990d; //output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.