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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:55:15+00:00 2026-05-13T12:55:15+00:00

I got trouble in creating special instance of member template function of non-template class.

  • 0

I got trouble in creating special instance of member template function of non-template class. I have, for example, class A with template member function F:

class A
   {public:
      template <class T> int F (T arg) const;
      ....
   }

and want to have a special instance of this template function F for type B:

class B;
...
template <> void A::F (B arg) const //GOOD!

and it works perfectly, until appears that B is a template itself!

This code

template <class T> class B ...
...
template <> void A::F (B<T> arg) const //error, T undeclared

as well as

template <class T> class B ...
...
template <class T> template <> void A::F (B<T> arg) const //error, too many templates

gives compiling error.

The second trouble is, how to declare this special instance (or template instance at whole) to be friend function of class B? (Is does not work even if B is not a template).

class B
   {friend template <> void A::F (B arg) const // error
     // as well as
    template <> friend void A::F (B arg) const // error
   }

Is there a way to write code in a way I’m going to at all or it is not possible?

  • 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-13T12:55:15+00:00Added an answer on May 13, 2026 at 12:55 pm

    You’re attempting to create a partial specialization for a function template, which is illegal. What you can do is simply create an overload.

    To create a friend, you merely have to use the correct syntax.

    The following compiles without errors.

    template <typename T>
    struct B {};
    
    struct A
    {
        template <typename T>
        void F(T arg) const;
    
        template <typename T>
        void F(B<T> arg) const;
    
        template <typename T>
        friend void G(B<T> arg);
    
        template <typename T>
        friend struct B;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got trouble on Member Description show up. For example, I have two laptops.
I have real trouble understanding mod_rewrite, and got some help in creating a RewriteRule
Why have I got trouble with charset on echo? $url = http://ru.wikipedia.org; $site=file_get_contents($url); echo
i'v got a trouble, i want to use Ckeditor with my project, i have
I have a little bit of trouble creating a menu in Rails. In ApplicationController
I have got trouble to bundle gems from git private repositories : gem 'test',
I've got some trouble with jQuery script on my Asp.net page. During first load
I've got some trouble with displaying pdfs in IE7 (which were generated by R&OS'
I've got a real trouble now. I've got my win Application and a lot
Got a crash report from a beta tester and I'm having trouble identifying the

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.