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

  • Home
  • SEARCH
  • 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 8109515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:26:33+00:00 2026-06-06T01:26:33+00:00

is it possible to somehow make a partial template specification a friend class? I.e.

  • 0

is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class

template <class T> class X{
    T t;
};

Now you have partial specializations, for example, for pointers

template <class T> class X<T*>{
    T* t;
};

What I want to accomplish is that every possible X<T*> is a friend class of X<S> for ANY S. I.e. X<A*> should be a friend of X<B>.

Of course, I thought about a usual template friend declaration in X:

template <class T> class X{
    template <class S> friend class X<S*>;
}

However, this does not compile, g++ tells me this:

test4.cpp:34:15: error: specialization of ‘template<class T> class X‘ must appear at namespace scope

test4.cpp:34:21: error: partial specialization ‘X<S*>‘ declared ‘friend’

Is this not possible at all or is there some workaround?

The reason why I am asking is that I need a constructor in X<T*> that creates this class from an arbitrary X<S> (S must be a subtype of T).

The code looks like this:

template <class T> class X<T*>{
    T* t;

    template<class S>
    X(X<S> x) : t(&(x.t))  {} //Error, x.t is private
}

Now, the compiler complains, of course, that x.t is not visibile in the constructor since it is private. This is why I need a partial specialization friend class.

  • 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-06T01:26:35+00:00Added an answer on June 6, 2026 at 1:26 am

    In C++, you can grant access beyond private on four levels.

    • completely public access (see pmr’s answer)
    • access within inheritance hierarchy (protected, irrelevant here)
    • to a base template friend (see this answer)
    • to a non-template or fully specialized friend (too weak to solve your use case)

    There is no middle way between the two latter kinds of friendship.

    From §14.5.4 of the C++ standard:.

    Friend declarations shall not declare partial specializations.

    The following declaration will allow you to implement what you need. It gives you a free hand to access any specialization of your template from any other specialization, but still only within X. It is slightly more permissive than what you asked for.

    template<class T> class X
    {
        template<class Any> friend class X;
        public:
            ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a IIS hosted service http://localhost/someProject/services/myservice.svc . Is it possible to somehow make
Following the example here http://www.java2s.com/Code/JavaAPI/javax.swing/JOptionPaneshowInputDialogComponentpObjectmStringtintmIconiObjectoObjecti.htm is it possible to somehow make the default jcombobox
I have following code: <a onclick=placesclick(); href=javascript:void(0)> <span class=place>3</span> </a> I need to make
Is it possible to somehow make Visual Studio / ASP.NET open multiple browsers when
I'm wondering is it possible somehow? sample code class CustomFilter { ... function FilterData($data,
Just wondering if it's possible somehow to make the CSS content property insert html
I was wondering if it is possible to somehow make parallel.for or parallel.foreach consume
I was wondering if it's possible to somehow make an application that supports Android
I'm asking myself Can you write a class template and a corresponding partial specialization
Is it possible somehow to ask the user to allow cross-domain access instead of

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.