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 591385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:37:45+00:00 2026-05-13T15:37:45+00:00

The following code should be self explanatory. I have two questions regarding the used

  • 0

The following code should be self explanatory. I have two questions regarding the used syntax (which is the syntax that must be used). I’ll be forever grateful if you could provide me with answers for these presented questions.

template <typename T>
struct A {
    template <typename S>
    void f (const A<S> &s);

    template <typename S>
    friend struct A; 
    // Question 1: why isn't the syntax 'friend struct A<S>' ? 
    // The semantic would stay, since we would like A<S> (for ANY S) to be friend of every A<T>..

    private:
        void g () const {}
};

template <typename T> 
template <typename S> // Question 2: Why can't the syntax be 'template <typename T, typename S>' ?
void A<T>::f (const A<S> &s) {
    s.g();
}

int main () {
    A<bool> abool;
    A<char> achar;

    abool.f(achar);
}

I have verified that indeed this is the only correct syntax (I’ll be happy to find out I’m mistaken). My questions are more regarding the reasoning behind the syntax, as explained in the questions’ body.

Thanks for any help.

  • 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-05-13T15:37:45+00:00Added an answer on May 13, 2026 at 3:37 pm

    why isn’t the syntax …

    Why can’t the syntax be …

    What do you expect us to say? Whoever decided this syntax (mostly Stroustrup himself, AFAIK) thought their syntax better than yours.

    Which one is nicer or easier to remember I wouldn’t know – but I do find theirs making more sense than yours. You’re free to disagree, of course.

    Edit: OK, Alexander has nicely answered question #2. About #1:

    The difference is that A<S> names a type, which is what is expected for a function argument, while A by itself is the name of a template, from which types are to be created, which makes sense if you want to befriend a template instead of a type:

    template <typename S>
    void f (const A<S> &s); // A<S> being the name of a type
    
    template <typename S>
    friend struct A; // A being the name of a template
    

    You can befriend a specific template instance instead of the whole template, but for that the template must already be known by the compiler (i.e., declared) at the friend declaration:

    template< typename T >
    class foo;
    
    class bar {
      friend class foo<int>; // foo<int> being the name of a specific instance of foo
    };
    

    So befriending a template is an exception (the "usual" friend declarations declares a function or class) and does need the differing syntax.

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

Sidebar

Related Questions

I have the following code which should be getting the text from a span
I have the following code which should put programs startable in Bash. if [
I have the following code which should make drawFrame be called every frame but
The following code should be pretty self-evident. What I have made up so far
I have used the following code to create a new thread: [NSThread detachNewThreadSelector:@selector(backgroundMethod:) toTarget:self
I have the following code (that should be correct): - (void) loadIntro:(ccTime)unused { [[GameManager
I have the following code to create an NSTimer which should update a label
The following code should display the id out of the JSON-object, which I got
I know that the following code should show and hide a tiny circular progress
I have the following code : class C: def __init__(self, dx = 1): self._dx

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.