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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:18:54+00:00 2026-05-23T21:18:54+00:00

I have a templated class with an templated member function template<class T> class A

  • 0

I have a templated class with an templated member function

template<class T>
class A {
public:
    template<class CT>
    CT function();
};

Now I want to specialize the templated member function in 2 ways. First for having the same type as the class:

template<class T>
template<>  // Line gcc gives an error for, see below
T A<T>::function<T>() {
    return (T)0.0;
}

Second for type bool:

template<class T>
template<>
bool A<T>::function<bool>() {
    return false;
}

Here is how I am trying to test it:

int main() {
    A<double> a;
    bool b = a.function<bool>();
    double d = a.function<double>();
}

Now gcc gives me for the line marked above:

error: invalid explicit specialization before ‘>’ token
error: enclosing class templates are not explicitly specialize

So gcc is telling me, that I have to specialize A, if I want to specialize function, right?
I do not want to do that, I want the type of the outer class to be open …

Is the final answer: it is not possible? Or is there a way?

  • 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-23T21:18:55+00:00Added an answer on May 23, 2026 at 9:18 pm

    Yes, this is the problem:

    error: enclosing class templates are not explicitly specialized 
    

    You cannot specialize a member without also specializing the class.

    What you can do is put the code from function in a separate class and specialize that, much like basic_string depends on a separate char_traits class. Then then non-specialized function can call a helper in the traits class.

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

Sidebar

Related Questions

I have a templated class like this: template <typename T> class AguiEvent { std::vector<std::tr1::function<void(T,
I have a templated class defined (in part) as template <class T> MyClass {
I have a template class where I want to use objects of that class
I have a C++ template class that gets instantiated with 3 different type parameters.
I have got a template class as follows: class MyClass<T> { T field; public
I have a class with several template member functions that I would like to
I have a templated class that is dependent on two template parameters to calculate
I have a mathematical matrix class. It contains a member function which is used
I have a dll that contains a templated class. Is there a way to
I have a template class that I serialize (call it C), for which I

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.