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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:47:04+00:00 2026-06-05T20:47:04+00:00

In a template, I want to drill down the template parameter to the real

  • 0

In a template, I want to drill down the template parameter to the real non-templated type. So:

template <typename T>
struct MyTemplate
{
    // sadly there's no extract_Base
    typedef typename extract_base<T>::MyType WorkType;
};
struct X {};
template <typename T> struct Templ {};
//MyTemplate<Templ<X>>::WorkType is X;
//MyTemplate<X>::WorkType is X;

The only solution I see is to define real base type like std::vector<X>::value_type is X. But I am curious if there’s a way to do this without defining auxiliary types inside each destination template.

I saw something like http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2965.html but this is draft? and I don’t quite get it.

Yes I know there’s multiple inheritance, but even for simple case this would be nice.

UPDATE: Nawaz solution works for me very well, and it is easy to extend to specific cases, e.g.

template<template<typename, typename> class X, typename T1, typename T2>
struct extract_base <X<T1, T2>>   //specialization
{
    typedef T1 base;
};

I can even apply is_base_of or other filters to T1/T2 and so on. So it does work for X<T,U> – at least with g++ 4.6.7.

  • 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-05T20:47:05+00:00Added an answer on June 5, 2026 at 8:47 pm

    First of all, let’s call it value_type instead of base, because value_type seems to be more appropriate term to describe the type which you want to extract.

    You can use this:

    template<typename T>
    struct extract_value_type //lets call it extract_value_type
    {
        typedef T value_type;
    };
    
    template<template<typename> class X, typename T>
    struct extract_value_type<X<T>>   //specialization
    {
        typedef T value_type;
    };
    

    It should work as long as the template argument to extract_value_type is of the form of either T or X<T>. It will not work for X<T,U>, however. But then it is easy to implement it in C++11 using variadic template.

    Use it as:

    template <typename T>
    struct MyTemplate
    {
        typedef typename extract_value_type<T>::value_type value_type;
    };
    

    Online demo : http://ideone.com/mbyvj


    Now in C++11, you can use variadic template to make extract_value_type work with class templates which take more than one template arguments, such as std::vector, std::set, std::list etc.

    template<template<typename, typename ...> class X, typename T, typename ...Args>
    struct extract_value_type<X<T, Args...>>   //specialization
    {
        typedef T value_type;
    };
    

    Demo : http://ideone.com/SDEgq

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

Sidebar

Related Questions

Suppose I'm in a template and I want to know if a type parameter
I want to do something like template <typename T> void foo(const T& t) {
I want a template to select from two types based on some condition. E.g.
I have a Django template that I want to extend in multiple places. In
I am creating a custom control. I want the template for this control to
I want to put template files in defferent directories. (foo/, bar/) How do you
I want to create a template class (let's call it Foo ) that only
I want to call and template and save the output to a variable. I
I want to do something like this: template<template<int d, class> class container, int dim
I want to have a template class that looks something like what I have

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.