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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:20:28+00:00 2026-05-30T18:20:28+00:00

I have a (free) function template that looks like this template <typename T> T

  • 0

I have a (free) function template that looks like this

template <typename T>
T get();

I now want to specialize this function for a class, which itself is a template. But my compiler doesn’t want to compile it, and I’m asking now if that is even possible and how I could achieve it. Just for the idea, the code could look as follows: (Doesn’t compile)

template <>
template <typename T>
foo_type<T> get<foo_type<T>>()
  • 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-30T18:20:30+00:00Added an answer on May 30, 2026 at 6:20 pm

    What you’re doing is called partial specialization of function template. But partial specialization of function template is not allowed. Overloading of function template is allowed, but in this case, it is not possible either, as the function has only return type, and overloading on return type is not allowed.

    So the solution is this:

    namespace details
    {
         template <typename T>
         struct worker
         {
             static T get();
         };
    
         template <typename T> //partial specialization of class is allowed
         struct worker<foo<T>>
         {
             static foo<T> get();
         };
    
    }
    
    template <typename T>
    T get()
    {
      return details::worker<T>::get();
    }
    

    You could also use overloads if you define them to take one argument so as to make overload valid:

    namespace details
    {
         template <typename T>
         static T get(T*); 
    
         template <typename T> 
         static foo<T> get(foo<T>*); //now the overload is valid
    
    }
    
    template <typename T>
    T get()
    {
      return details::get<T>(static_cast<T*>(0));
    }
    

    Note that the argument static_cast<T*>(0) is used to help the compiler to select the correct overload. If T is other than foo<U>, then the first overload will be selected as the type of the argument passed to it will be T* as opposed to foo<U>*. If T is foo<U>, then the second overload will be selected by the compiler because it is more specialized, and can accept the argument passed to it which is foo<U>* in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got class with template methods that looks at this: struct undefined {}; template<typename
I have a template base class like this: template<typename T, std::size_t Size> class VectorT
Suppose I have a free function called InitFoo . I'd like to protect this
I have a function that is side-effect free. I would like to run it
I have a view and collection like this: window.DmnView = Backbone.View.extend({ template: _.template($(#tmpl_dmnListItem).html()), events:
I have a free function as part of a class. In the constructor for
I have a Exception base class which defines a stream function: class Exception {
I have a free App that I am about to update. After the update
I've learned in College that you always have to free your unused Objects but
We will develop a web site that will have some free services and we

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.