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

The Archive Base Latest Questions

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

I understand how to create type traits and then specialise for a particular class,

  • 0

I understand how to create type traits and then specialise for a particular class, but in my case I would like to specialise for a class template. The code below does not compile, but the idea is that the specialisation of Traits for MyTemplatisedClass should work for which ever type the user decides to use with MyTemplatisedType.

class Traits
{
public:
    static bool someProperty(void) { return false; }
};

template<typename Type>
class MyTemplatisedClass
{
};

template<typename Type>
template<>
class Traits< MyTemplatisedClass<Type> >
{
public:
    static bool someProperty(void) { return true; }
};

int main(int argc, char* argv[])
{
    std::cout << Traits< MyTemplatisedClass<float> >::someProperty() <<std::endl; //This should be true
    return 0;
}

Is this possible or am I asking too much? According to the compiler the first problem is

error C2989: 'Traits' : class template has already been declared as a non-class template    

Which is correct, but how do I fix this? If it makes any difference I don’t need it to work for non-templatised classes, just templatised ones is fine. Edit: Actually it would be nice if it worked for both templatised and non-templatised classes.

  • 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-27T18:24:52+00:00Added an answer on May 27, 2026 at 6:24 pm

    Traits needs to be a template in order to be specialized.

    In the specialization drop the line with empty <>: this isn’t a template nested within a template or something.

    template <typename Type> //can only specialize templates
    class Traits
    {
    public:
        static bool someProperty(void) { return false; }
    };
    
    template<typename Type>
    class MyTemplatisedClass
    {
    };
    
    template<typename Type>
    //template<>  //Too much here
    class Traits< MyTemplatisedClass<Type> >
    {
    public:
        static bool someProperty(void) { return true; }
    };
    

    But if you meant the specialization for any template with one type argument, then that would be:

    template < template <class> class SomeTemplatizedType, class Type>
    //         ^^^^^^^^^^^^^^^^^^^^^^
    //         names a template, not type
    class Traits< SomeTemplatizedType<Type> >;
    //            ^^^^^^^^^^^^^^^^^^^   ^
    //             template name        |
    //                               argument
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a Driver type class where, below, Base is the Driver that
I understand that you can now create MVC-specific user controls, but will my existing
I understand that I can call ToString().IndexOf(...), but I don't want to create an
After being taught how to create a hash table in class, I don't understand
I understand that bigint is not a function but rather a type constructor. That's
I'm trying to create what I understand to be a Class Factory in Delphi
How would I create a type while knowing just the name of the type
I understand that you can create a new class with parameters from a string
I have a file with 3 create type statements. I do not understand why,
I understand that struct is value type and class is reference type in .Net.

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.