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

The Archive Base Latest Questions

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

What is the C++ syntax for specializing a template function that’s inside a template

  • 0

What is the C++ syntax for specializing a template function that’s inside a template class? For example, consider that I have the following two classes and their usage. I would like to be able to provide specialized implementations of the method X::getAThing() for different types. E.g.: int, std::string, arbitrary pointer or class, etc.

template <class c1> class X {
public:
   template<typename returnT> returnT getAThing(std::string param);
   static std::string getName();
private:
   c1 theData;
};

// This works ok...
template <class c1> std::string X<c1>::getName() {
   return c1::getName();
}

// This blows up with the error:
// error: prototype for 'int X<c1>::getAThing(std::string)' does not match any in class 'X<c1>'
template <class c1> template <typename returnT> int X<c1>::getAThing(std::string param) {
   return getIntThing(param); // Some function that crunches on param and returns an int.
}

// More specialized definitions of getAThing() for other types/classes go here...

class Y {
public:
   static std::string getName() { return "Y"; }
};

int main(int argc, char* argv[])
{
   X<Y> tester;
   int anIntThing = tester.getAThing<int>(std::string("param"));
   cout << "Name: " <<  tester.getName() << endl;
   cout << "An int thing: " << anIntThing << endl;
}

I’ve been trying to guess at the correct syntax for the specialization for at least an hour, and can’t figure anything out that will compile. Any help would be greatly appreciated!

  • 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-20T04:33:29+00:00Added an answer on May 20, 2026 at 4:33 am

    So, I’m taking a different approach to answering your question. I’m going to start from something that sort of does what you want, and works. And then maybe we can figure out how to permute it into something closer to what you really want:

    #include <string>
    #include <iostream>
    
    int getIntThing(const ::std::string &param);
    
    template <typename returnT>
    returnT getThingFree(const ::std::string &param);
    
    template <>
    int getThingFree<int>(const ::std::string &param)
    {
       return getIntThing(param);
    }
    
    // More specialized definitions of getAThing() for other types/classes
    // go here...
    
    template <class c1> class X {
    public:
       template<typename returnT> returnT getAThing(std::string param);
       static std::string getName();
    private:
       c1 theData;
    };
    
    // This works ok...
    template <class c1> std::string X<c1>::getName() {
       return c1::getName();
    }
    
    // This also works, but it would be nice if I could explicitly specialize
    // this instead of having to explicitly specialize getThingFree.
    template <class c1>
    template <class RT>
    RT X<c1>::getAThing(std::string param) {
       // Some function that crunches on param and returns an RT.
       // Gosh, wouldn't it be nice if I didn't have to redirect through
       // this free function?
       return getThingFree<RT>(param);
    }
    
    class Y {
    public:
       static std::string getName() { return "Y"; }
    };
    
    int main(int argc, char* argv[])
    {
       using ::std::cout;
       X<Y> tester;
       int anIntThing = tester.getAThing<int>(std::string("param"));
       cout << "Name: " <<  tester.getName() << '\n';
       cout << "An int thing: " << anIntThing << '\n';
    }
    

    Here is another idea that sort of works, and isn’t exactly what you want, but is closer. I think you’ve thought of it yourself. It’s also rather ugly in the way it uses type deduction.

    #include <string>
    #include <iostream>
    
    template <class c1> class X;
    
    int getIntThing(const ::std::string &param)
    {
       return param.size();
    }
    
    // You can partially specialize this, but only for the class, or the
    // class and return type. You cannot partially specialize this for
    // just the return type. OTOH, specializations will be able to access
    // private or protected members of X<c1> as this class is declared a
    // friend.
    template <class c1>
    class friendlyGetThing {
     public:
       template <typename return_t>
       static return_t getThing(X<c1> &xthis, const ::std::string &param,
                                return_t *);
    };
    
    // This can be partially specialized on either class, return type, or
    // both, but it cannot be declared a friend, so will have no access to
    // private or protected members of X<c1>.
    template <class c1, typename return_t>
    class getThingFunctor {
     public:
       typedef return_t r_t;
    
       return_t operator()(X<c1> &xthis, const ::std::string &param) {
          return_t *fred = 0;
          return friendlyGetThing<c1>::getThing(xthis, param, fred);
       }
    };
    
    template <class c1> class X {
    public:
       friend class friendlyGetThing<c1>;
    
       template<typename returnT> returnT getAThing(std::string param) {
          return getThingFunctor<c1, returnT>()(*this, param);
       }
       static std::string getName();
    private:
       c1 theData;
    };
    
    // This works ok...
    template <class c1> std::string X<c1>::getName() {
       return c1::getName();
    }
    
    class Y {
    public:
       static std::string getName() { return "Y"; }
    };
    
    template <class c1>
    class getThingFunctor<c1, int> {
     public:
       int operator()(X<c1> &xthis, const ::std::string &param) {
          return getIntThing(param);
       }
    };
    
    // More specialized definitions of getAThingFunctor for other types/classes
    // go here...
    
    int main(int argc, char* argv[])
    {
       using ::std::cout;
       X<Y> tester;
       int anIntThing = tester.getAThing<int>(std::string("param"));
       cout << "Name: " <<  tester.getName() << '\n';
       cout << "An int thing: " << anIntThing << '\n';
    }
    

    I would recommend declaring getThingFunctor and friendlyGetThing in a semi-private utility namespace.

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

Sidebar

Related Questions

I have a class template nested inside another template. Partially specializing it is easy:
The following syntax is the current syntax I have that's works. $dbh = connect();
In the following, am I forgetting some correct syntax for partial specializing class NumInfo
What syntax for MySQL would I use to drop multiple tables that have a
The following syntax is present in a .js file. var fun1 = function(fun1_parameter1){ return{
I have implemented basic syntax highlighting by properly setting the NSTextStorage delegate of my
Parse error: syntax error, unexpected T_STRING in app/design/frontend/base/default/template/catalog/product/new.phtml on line 37: <?php echo $this->getReviewsSummaryHtml($_product,
Can anyone offered syntax for a sql query that takes an already created table
I've found syntax highlighters that highlight pre-existing code, but I'd like to do it
What syntax highlighter is GitHub using on their site to display the code when

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.