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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:37:02+00:00 2026-06-01T14:37:02+00:00

I have a problem with nested templates and their template specialization. Given the following

  • 0

I have a problem with nested templates and their template specialization. Given the following classes:

A small template class

template<class U>
class T {
public:
    T(){}
    virtual ~T (){}

};

And some kind of nested template

template<typename T, template<typename> class U>
class A {
public:
    void foo() 
    { 
        std::cerr << "A generic foo"; 
    }
};

And a small main.cpp

int main(int argc, const char *argv[])
{
    A<int,T> *a = new A<int,T>;
    a->foo();

    //This wont work:
    A<double,T*> *b = new A<double,T*>;
    b->foo();

    return 0;
}

Now I need a specialization if U is a pointer:

    A<double,T*> *b = new A<double,T*>;
    b->foo();

How to achieve this? I tried something like:

template<typename T, template<typename> class U>
class A< T, U* >
{
public:
void foo()
{
    std::cerr << "A specialized foo";
}
};

But it just resolves in

A.h:18:16: Error: Templateargument 2 is invalid
  • 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-01T14:37:03+00:00Added an answer on June 1, 2026 at 2:37 pm

    What you’re tying to do is not possible, because T* has no meaning. Neither is it a proper type, nor does it match a template, which requires additional parameters. If U were to represent T*, what would U<int> be? You probably mean T<int>* but that doesn’t match your declaration, so there is no way to plug that type into A.

    Since you asked for a way to get around this, from the top of my head, something like this.

    Accept a third template argument to A, which I would call Expander and set it by default to this:

    template <typename T> struct Expander {
      typedef T type;
    };
    

    Then, when invoking A you could say

    A<int,T> normal;
    A<int,T,PtrExpander> pointer;
    

    with

    template <typename T> struct PtrExpander {
      typedef T* type;
    };
    

    and A would be:

    template<typename T, template<typename> class U, template <typename> class E = Expander> class A {
      typedef typename E<U<Your_Args_to_U> >::type;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing several class templates that contain nested iterator classes, for which
I have nested iterator in my custom stack template class. The problem I get
I'm having a problem with some for nested loops that I have to convert
I have a problem concerning nested templates and the overriding of the assignment operator.
I have some problem with the nested try statements in java and I am
I have a problem where the nested templates are being evaluated before the parent
I have a small template class with a non-static member of type boost::shared_mutex .
I have this problem: After instantiating this Window, open all nested expanders on the
Is it possible to tie nested generics/captures together? I often have the problem of
I have a nested function to show/hide paragraphs news-ticker-style. The problem is that when

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.