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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:25:33+00:00 2026-06-18T08:25:33+00:00

I have the following traits class template<typename T> struct FeatureType; and I’m using it

  • 0

I have the following traits class

template<typename T> struct FeatureType;

and I’m using it like so, which works fine:

class Foo { };
template<> struct FeatureType<Foo> {
  typedef int value;
};

template<typename T> class Bar { };
template<typename T> struct FeatureType<Bar<T>> {
  typedef T value;
};

Is there a way to extent this implementation for generic types to those that have more than one type parameter (unlike Bar above)? The following does not work

template<typename A, typename B> class Huh { };
template<typename A, typename B> struct FeatureType<Huh<A,B>> {
  typedef A value;
};

Thanks!

  • 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-06-18T08:25:34+00:00Added an answer on June 18, 2026 at 8:25 am

    Regular templates

    Regular templates do not overload on their template parameters, but you can partially specialize them on arbitrary many template parameters. Your code should work as long as you put ; behind every struct declaration/definition. (note it is a custom to denote nested types inside templates as type, and values as value):

    #include <iostream>
    
    template<typename T>
    struct FeatureType;
    
    class Foo { };
    template<> struct FeatureType<Foo> 
    {
      typedef int type;
      type value;
    };
    
    template<typename T> class Bar { };
    template<typename T> struct FeatureType<Bar<T>> 
    {
      typedef T type;
      type value;
    };
    
    template<typename A, typename B> class Huh {};
    template<typename A, typename B>
    struct FeatureType< Huh<A,B> > 
    { 
       typedef A type; 
       type value;
    };
    
    int main()
    {
      FeatureType<Foo> f0;
      f0.value = 0;
    
      FeatureType< Bar<int> > f1;
      f1.value = 1;
    
      FeatureType< Huh<int, int> > f2;
      f2.value = 2;
    
      std::cout << f0.value << f1.value << f2.value;   
    }
    

    Output on LiveWorkSpace (gcc 4.7.2)

    Note: even if you have multiple formal template parameters (A, B, or as many as you like), the actual template is partially specialized for the single class Huh<A, B>

    Variadic templates

    If you actually want to have multiple versions of FeatureType taking a different number of template parameters, you need to use variadic templates (C++11)

    #include <iostream>
    
    template<typename... Args>
    struct FeatureType;
    
    template<> struct FeatureType<int> 
    {
      typedef int type;
      type value;
    };
    
    template<typename T> struct FeatureType< T > 
    {
      typedef T type;
      type value;
    };
    
    template<typename A, typename B>
    struct FeatureType< A, B > 
    { 
       typedef A type; 
       type value;
    };
    
    int main()
    {
      FeatureType< int > f0;
      f0.value = 0;
    
      FeatureType< int > f1;
      f1.value = 1;
    
      FeatureType< int, int > f2;
      f2.value = 2;
    
      std::cout << f0.value << f1.value << f2.value;   
    }
    

    Output on LiveWorkSpace

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

Sidebar

Related Questions

I have the following setup (which works fine). Using CodeFirst (CTP4). A template has
I have the following templated class structure struct TraitA{}; struct TraitB{}; template<typename trait> struct
For instance, I have just found myself writing the following traits like class: template<class
I have the following template class: template<class T> class C { typedef C_traits<T> traits;
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray
Suppose we have the following template class template<typename T> class Wrap { /* ...
I have a struct which indicates a trait: template<typename T> struct FooTraits { static
The following code, which attempts to specialize class template 'special', based on the return
I have the following code: #include <map> #include <string> class policy1 { public: struct
I'm using Visual Studio 2010. I have a class with the following constructor: CVideoAnnotation::CVideoAnnotation(std::string

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.