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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:28:23+00:00 2026-05-28T19:28:23+00:00

I want to specialize templates based on their inner arguments. I am using non-strict

  • 0

I want to specialize templates based on their inner arguments. I am using non-strict evaluation which makes things difficult.

The specializations should be based off of the least nested pattern match. For instance:

template<typename T>
struct data1;

template<typename T>
struct fun1 {
  using type = data1<T>;
};

template<typename T>
struct fun2;
template<typename T>
struct fun2<data1<T>> {
  using type = data1<T>;
};

fun2<data1<int>> x1;             // this works as expected, T=int
fun2<data1<fun1<int>>>::type x2; // this works as expected, T=fun1<int>
fun2<fun1<int>>::type x3;        // this should be specialized as fun2<data1<int>>, T=int
fun2<fun2<fun1<int>>>::type x4;  // this should be specialized as fun2<data1<int>>, T=int

How can I do this?

  • 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-28T19:28:24+00:00Added an answer on May 28, 2026 at 7:28 pm

    You can just use a template template parameter:

    template<typename T>
    struct data1;
    
    template<typename T>
    struct fun1 {
      using type = data1<T>;
    };
    
    template<typename T>
    struct fun2;
    
    template<class T>
    struct fun2<data1<T>>{
      using type = data1<T>;
    };
    
    template<template<class> class X, class T>
    struct fun2<X<T>>
      : fun2<typename X<T>::type>{};
    

    Tests:

    #include <type_traits>
    
    static_assert(std::is_same<fun2<data1<int>>::type, data1<int>>::value, "fun2<data1<int>>");
    static_assert(std::is_same<fun2<data1<fun1<int>>>::type, data1<fun1<int>>>::value, "fun2<data1<fun1<int>>>");
    static_assert(std::is_same<fun2<fun1<int>>::type, data1<int>>::value, "fun2<fun1<int>>");
    static_assert(std::is_same<fun2<fun2<fun1<int>>>::type, data1<int>>::value, "fun2<fun2<fun1<int>>>");
    
    int main(){
    }
    

    Live example on Ideone (with using-aliases changed to typedefs).

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

Sidebar

Related Questions

I want to create a simple integer range checker and converter using c++ templates.
Bumped into another templates problem: The problem: I want to partially specialize a container-class
I want to be able to specialize based on a constant c style string.
I want to specialize a template for STL's vector template arguments. Something like this:
I have a templated function which I want to specialize foo to const char[N]
i want a specialize template in a pointer-to-member-function case. Is there a way to
I want to make this specialized w/o changing main. Is it possible to specialize
I want to specialize operator<< but this code is not compiling; template<> std::ostream& operator<<
I want to specialize a class template with the following function: template <typename T>
I have a set of specialized templates. Now I want to create specialized templates

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.