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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:00:21+00:00 2026-06-13T16:00:21+00:00

Say I have two Template classes. template<class T> class baseclass1 { template<class> friend class

  • 0

Say I have two Template classes.

template<class T>
class baseclass1
{
    template<class> friend class baseclass2;
}

template<class D>
class baseclass2
{
     template<class T> void foo( D& x, T& y)
     {
          ...
     }
}

The Above code allows all types of baseclass1 to friend all types of baseclass2, a many-to-many relationship. I have two questions,

What is the syntax to allow baseclass1 to friend just the function

baseclass2<class D>::foo<class T>( D& x, T& y).  

And, what is the syntax to allow baseclass1 to friend just the function

baseclass2<class D>::foo<class T>( D& x, T& y) where T from baseclass1 matches The T from Function foo.

EDIT

To those who keep claiming you can’t friend a template specialization. This code works

template<class cake>
class foo
{
    public:
        static void bar(cake x)
        {
            cout << x.x;
        }
};


class pie
{
    public:
        void set( int y){ x = y; }
    private:
        int x;

        friend void foo<pie>::bar(pie x);
};

class muffin
{
    public:
        void set( int y){ x = y; }
    private:
        int x;

    friend void foo<pie>::bar(pie x);
};

int main
{
        pie x;
        x.set(5);
        foo<pie>::bar(x);

        muffin y;
        y.set(5);
        //foo<muffin>::foo(y); //Causes a compilation Error because I only friended the pie specialization
}

Even notice where muffin friends the wrong foo, and still causes a compilation error. This works with both functions and classes. I am totally willing to accept that this isn’t possible in my specific situation (It’s actually looking more and more that way) I’d just like to understand why.

  • 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-13T16:00:22+00:00Added an answer on June 13, 2026 at 4:00 pm

    Befriending all possible specializations of baseclass2<D>::foo is rather easy:

    template<class T> class baseclass1;
    
    template<class D>
    class baseclass2{
    public:
      template<class T>
      void foo(D&, T&){ baseclass1<T> x; x.priv_foo(); }
    };
    
    template<class T>
    class baseclass1{
      template<class D>
      template<class U>
      friend void baseclass2<D>::foo(D&, U&);
    
      void priv_foo(){}
    };
    
    template<class T>
    class baseclass1{
      template<class D>
      template<class U>
      friend void baseclass2<D>::foo(D&, U&);
    };
    

    Live example.

    A forward declaration of baseclass2 (so baseclass1 knows that baseclass2 exists and is a template) and two templates, one for the class, one for the function. It also looks like this for out-of-class definitions for function templates of class templates. 🙂

    Befriending specifically baseclass2<D>::foo<T> is not possible, however, or I can’t find
    the correct syntax for it.

    A workaround might be some global function that forwards the access and together with the passkey pattern, but meh, it’s a mess (imho):

    template<class D> class baseclass2;
    
    template<class D, class T>
    void baseclass2_foo(baseclass2<D>& b, D&, T&);
    
    template<class D, class T>
    class baseclass2_foo_key{
      baseclass2_foo_key(){} // private ctor
      friend void baseclass2_foo<>(baseclass2<D>&, D&, T&);
    };
    
    template<class T>
    class baseclass1{
    public: // public access, but only baseclass2_foo can create the key
      template<class D>
      void priv_foo(baseclass2_foo_key<D, T> const&){}
    };
    
    template<class D, class T>
    void baseclass2_foo(baseclass2<D>&, D&, T&){
      baseclass1<T> x;
      x.priv_foo(baseclass2_foo_key<D, T>());
    }
    
    template<class D>
    class baseclass2{
    public:
      template<class T>
      void foo(D& d, T& t){ baseclass2_foo(*this, d, t); }
    };
    

    Live example.

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

Sidebar

Related Questions

Say I have two separate classes, A and B. I also have Repository class
Let say we have a base class and its two derived classes; The base
I have a template class, say: template<class T> class someClient { void someCallbackA() {foo_->onA();}
Let's say I have two classes Foo and Bar, and I want to make
Say I have a function with template type T and two other classes A
Let's say I have the following two classes: public class TestResults { public string
Say I have two classes and have a requirement that the primary key property
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Say you have two classes, A and B. Is it possible to instantiate both
ok, say you have two dbs. one you use as a master template which

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.