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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:08:45+00:00 2026-06-02T03:08:45+00:00

It is possible to specialize some class member function outside the template definition: template<class

  • 0

It is possible to specialize some class member function outside the template definition:

template<class A>
struct B {
   void f();   
};

template<>
void B<int>::f() { ... }

template<>
void B<bool>::f() { ... }

and in this case I can even omit definition of function f for a general type A.

But how to put this specializations inside the class? Like this:

template<class A>
struct B {
   void f();   

   void f<int>() { ... }
   void f<bool>() { ... }
};

What syntax should I use in this case?

EDIT:
For now the solution with fewest lines of code is to add a fake template function f definition and explicitly call it from original function f:

template<class A>
struct B {
   void f() { f<A>(); }

   template<class B> 
   void f();

   template<> 
   void f<int>() { ... }

   template<> 
   void f<bool>() { ... }
};
  • 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-02T03:08:46+00:00Added an answer on June 2, 2026 at 3:08 am

    You can make B::f a template function within your struct:

    struct B {
        template <typename T>
        void f();
    
        template<>
        void f<int>() { ... }
    
        template<>
        void f<bool>() { ... }
    };
    

    Edit:

    According to your comment this may help you, but I’ve not tested if it works:

    template <typename A>
    struct B {
        template <typename T = A>
        void f() { ... }
    
        template<>
        void f<int>() { ... }
    
        template<>
        void f<bool>() { ... }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to specialize this template for any basic_string's? template<class T> struct X
I want to specialize following member function: class foo { template<typename T> T get()
I have this functor: struct functor { template<class T> void operator()(T value) // (*)
Consider the following: struct A { typedef int foo; }; struct B {}; template<class
I'm currently refactoring some code the explicitly specializes a member function of a class
I want to make this specialized w/o changing main. Is it possible to specialize
I have a class structure which is template<int T> class MyClass { public: MyClass(){};
I would like to define a nullary static template member function which would be
Is it possible via an attribute of some sort to serialize a string as
Is it possible to specialize a templatized method for enums? Something like (the invalid

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.