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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:51:19+00:00 2026-05-23T18:51:19+00:00

I have a situation similar to this: template<class A, class B> class MyClass<A, B>

  • 0

I have a situation similar to this:

template<class A, class B>
class MyClass<A, B>
{
  ...
  static A RARELY_USED_A;
}

// Seems to work but does not cover all possible cases, since 
// there may be instances of A that have no numeric limits.
template<class A, class B>
A MyClass<A, B>::RARELY_USED_A= std::numeric_limits<A>::max();

From what I saw this seems to work. However, strings may be used as A under some circumstances and so I thought I’d simply create a specialization for this special case.

// Does not complile
template<class B>
string MyClass<string, B>::RARELY_USED_A= "";

unfortunately this does not complie properly with error msg:

error: template definition of non-template 'std::string MyClass<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, B>::RARELY_USED_A'

Note that, on the othre hand, a full specialization seems to work (untested at runtime but compiles)

// This complies but is not gernic enough and hence useless to me
template<>
string MyClass<string, string>::RARELY_USED_A= "";

I assume, I must be doing something wrong. I would be really grateful if you could point out what exactly it is. I thought partical specializations were supposed to work this way.

Thanks a lot in advance.

e: edited the name of the DEFAULT_A to RARELY_USED_A, because I thought that “default” was misleading somehow

  • 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-23T18:51:19+00:00Added an answer on May 23, 2026 at 6:51 pm

    Use inheritance to reuse and specialize without duplicating all the common code:

    template<typename A>
    struct RarelyUsedShared
    {
        static A RARELY_USED_A;
    };
    
    template<typename A>
    A RarelyUsedShared<A>::RARELY_USED_A = std::numeric_limits<A>::max();
    
    template<>
    string RarelyUsedShared<string>::RARELY_USED_A = "";
    
    template<typename A, typename B>
    class MyClass<A, B> : RarelyUsedShared<A>
    {
      ...
    };
    

    Note that this will result in sharing the member across various B, which is ok if the member should be const. If not, the helper can take two template parameters, and you can partially specialize it:

    template<typename A, typename B>
    struct RarelyUsedNotShared
    {
        static A RARELY_USED_A;
    };
    
    template<typename A, typename B>
    A RarelyUsedNotShared<A, B>::RARELY_USED_A = std::numeric_limits<A>::max();
    
    template<typename B>
    struct RarelyUsedNotShared<string, B>
    {
        static A RARELY_USED_A;
    };
    
    typename<typename B>
    string RarelyUsedNotShared<string, B>::RARELY_USED_A = "";
    
    template<typename A, typename B>
    class MyClass<A, B> : RarelyUsedNotShared<A, B>
    {
      ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a similar situation like this one @Entity @Indexed public class Place {
I have seen a similar related post but my situation is a little different
i have situation like this: class IData { virtual void get() = 0; virtual
I have a similar situation to this question , which was already posted. Koraktor
I have a very similar situation to what's described in this question : Engineer
I know there are questions similar to this, but none address my specific situation.
I have a similar situation to this question . I have a custom sequential
I have a situation very similar to the one in this question: Selective Cache
Something similar has been asked in maven user list but I have the situation,
I have a very similar situation to the person who asked: Can I serve

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.