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

The Archive Base Latest Questions

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

Possible Duplicate: Specialization of templated member function in templated class template <class T> class

  • 0

Possible Duplicate:
Specialization of templated member function in templated class

template <class T>    
class MyClass
{
   template <int N>
   void func() {printf("unspecialized\n");}
};
template<class T>
template<>
MyClass<T>::func<0>()
{
   printf("specialzied\n");
}

This does not work. Is it possible to specialize a template method of an template 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-02T03:31:59+00:00Added an answer on June 2, 2026 at 3:31 am

    It cannot be done as requested. For some reason (I’m not sure about the rationale) explicit (i.e full) specialization of a member template is only allowed when the enclosing class is also explicitly (i.e fully) specialized. This requirement is explicitly spelled out in the language standard (see 14.7.3/18 in C++98, C++03 and 14.7.3/16 in C++11).

    At the same time, partial specializations of member class templates are allowed, which in many cases can be used as a workaround (albeit an ugly one). But, obviously, it is only applicable to member class templates. When it comes to member function templates, an alternative solution has to be used.

    For example, a possible workaround is to delegate the call to a static member of a template class and specialize the class instead (which is often recommended as a better idea than specialization of function templates http://www.gotw.ca/publications/mill17.htm)

    template <class T>    
    class MyClass
    {
       template <int N, typename DUMMY = void> struct Func {
         static void func() { printf("unspecialized\n"); }
       };
    
       template <typename DUMMY> struct Func<0, DUMMY> {
         static void func() { printf("specialized\n"); }
       };
    
       template <int N> void func() { Func<N>::func(); }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Where and why do I have to put the “template” and “typename”
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: How do I serialize an enum value as an int? Hi, all!
Possible Duplicate: Serializable Inheritance Is serialization inheritable? Example: [Serializable] class A { } class
Possible Duplicate: Is it possible to write a C++ template to check for a
Possible Duplicate: Implicit cast from char** to const char** Given the following code: void
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is 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.