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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:10:24+00:00 2026-06-13T01:10:24+00:00

How can I specialize or overload function func so that specialization handles all instances

  • 0

How can I specialize or overload function func so that specialization handles all instances of MyClass? Assume func is a librarian function (like std::swap for example) so I can’t change func, and I can’t replace or wrap it, I have to specialize it.

#include <iostream>

template<typename T>
class MyClass
{
};

template<typename T>
void func(const T&)
{
    std::cout << "Default" << std::endl;
}

// I don't want to copy this stuff for every instance of MyClass
template<>
void func<MyClass<int>>(const MyClass<int>&)
{
    std::cout << "Specialization" << std::endl;
}

int main(int, char**)
{
    func(int(0)); // "Default"

    func(MyClass<int>()); // "Specialization"

    func(MyClass<double>()); // "Default" but I want "Specialization" here

    return 0;
}
  • 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-13T01:10:25+00:00Added an answer on June 13, 2026 at 1:10 am

    Here are a few examples of what you can do.

    template<typename T>
    void func(const T&)
    {
        std::cout << "Default" << std::endl;
    }
    
    template<template<typename> class A , typename B>
    void func(const A<B>&)
    {
        std::cout << "Overload All templated classes with one templated argument B" << std::endl;
    }
    
    template<typename T>
    void func(const MyClass<T>&)
    {
        std::cout << "Overload Myclass with some templated argument T" << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly
The usual definition for a specialization of a template function is something like this:
How can i specialize a generic function to take symbols designating subclasses of given
I want to partially specialize an existing template that I cannot change ( std::tr1::hash
I know from this thread - template class member function only specialization that if
I need to specialize the hash function for unordered_map so I can use int
Is something like this possible? // We can even assume T and U are
Given a template template <int n> void f(){...}; I know I can specialize it
I' m re-implementing std::map . I need to make sure that any data type
Template specialization does not take into account inheritance hierarchy. For example, if I specialize

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.