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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:32:58+00:00 2026-05-15T05:32:58+00:00

I’m trying to write code that uses a member typedef of a template argument,

  • 0

I’m trying to write code that uses a member typedef of a template argument, but want to supply a default type if the template argument does not have that typedef. A simplified example I’ve tried is this:

struct DefaultType    { DefaultType()    { printf("Default ");    } };
struct NonDefaultType { NonDefaultType() { printf("NonDefault "); } };

struct A {};
struct B { typedef NonDefaultType Type; };

template<typename T, typename Enable = void> struct Get_Type { 
    typedef DefaultType Type; 
};
template<typename T> struct Get_Type< T, typename T::Type > {
    typedef typename T::Type  Type; 
};

int main()
{
    Get_Type<A>::Type test1;
    Get_Type<B>::Type test2;
}

I would expect this to print “Default NonDefault”, but instead it prints “Default Default”. My expectation is that the second line in main() should match the specialized version of Get_Type, because B::Type exists. However, this does not happen.

Can anyone explain what’s going on here and how to fix it, or another way to accomplish the same goal?

Thank you.

Edit:

Georg gave an alternate method, but I’m still curious about why this doesn’t work. According the the boost enable_if docs, a way to specialize a template for different types is like so:

template <class T, class Enable = void> 
class A { ... };

template <class T>
class A<T, typename enable_if<is_integral<T> >::type> { ... };

template <class T>
class A<T, typename enable_if<is_float<T> >::type> { ... };

This works because enable_if< true > has type as a typedef, but enable_if< false > does not.

I don’t understand how this is different than my version, where instead of using enable_if I’m just using T::Type directly. If T::Type exists wouldn’t that be the same as enable_if< true >::type in the above example and cause the specialization to be chosen? And if T::Type doesn’t exist, wouldn’t that be the same as enable_if< false >::type not existing and causing the default version to be chosen in the above example?

  • 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-15T05:32:59+00:00Added an answer on May 15, 2026 at 5:32 am

    To answer your addition – your specialization argument passes the member typedef and expects it to yield void as type. There is nothing magic about this – it just uses a default argument. Let’s see how it works. If you say Get_Type<Foo>::type, the compiler uses the default argument of Enable, which is void, and the type name becomes Get_Type<Foo, void>::type. Now, the compiler checks whether any partial specialization matches.

    Your partial specialization’s argument list <T, typename T::Type> is deduced from the original argument list <Foo, void>. This will deduce T to Foo and afterwards substitutes that Foo into the second argument of the specialization, yielding a final result of <Foo, NonDefaultType> for your partial specialization. That doesn’t, however, match the original argument list <Foo, void> at all!

    You need a way to yield the void type, as in the following:

    template<typename T>
    struct tovoid { typedef void type; };
    
    template<typename T, typename Enable = void> struct Get_Type { 
        typedef DefaultType Type; 
    };
    template<typename T> 
    struct Get_Type< T, typename tovoid<typename T::Type>::type > {
        typedef typename T::Type  Type; 
    };
    

    Now this will work like you expect. Using MPL, you can use always instead of tovoid

    typename apply< always<void>, typename T::type >::type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.