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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:12:05+00:00 2026-05-11T21:12:05+00:00

Be forewarned: This question seems way more obvious than it actually is. I’d like

  • 0

Be forewarned: This question seems way more obvious than it actually is.

I’d like to write a template that can accept any concrete class or template class as a template parameter. This might seem useless because without knowing whether the passed in T is templated or not you won’t know how to use it. The reason I want this is so that I can declare a general template with no definition, that users then specialize. Because users are specializing it, they always know about the type they’re dealing with. But users can’t specialize a template without it first being declared.

You could do this:

template<class T>
class myclass;

But that won’t work if you pass in a templated T, for example myclass<std::vector> won’t work. So then we try this:

template<class T>
class myclass;

template<template<class> T>
class myclass;

This might be the right track, but it won’t work as is because class templates can’t be overloaded. So let’s switch it to function templates, which can be:

template<class T>
void myfunc();

template<template<class> T>
void myfunc();

Sweet, so we’re done right? Well, there might be different numbers of parameters given to the template template parameter, so we’ll need to take that into account too.

template<class T>
void myfunc();

template<template<class> T>
void myfunc();

template<template<class, class> T>
void myfunc();

template<template<class, class, class> T>
void myfunc();

// etc.

Ugly, but the Boost Preprocessor Library can generate this code for us (and in C++0x support for variadic templates will be added so this ugliness is only temporary). But we’ve still forgotten a case! What if one of T’s parameters isn’t a class, but a constant integer. Lets try to support that:

template<class T>
void myfunc();

template<template<class> T>
void myfunc();

template<template<class, class> T>
void myfunc();

template<template<class, class, class> T>
void myfunc();

// etc.

template<template<class> T>
void myfunc();

template<template<class, int> T>
void myfunc();

template<template<int, class> T>
void myfunc();

template<template<int, class, class> T>
void myfunc();

template<template<class, int, class> T>
void myfunc();

template<template<class, class, int> T>
void myfunc();

// etc.

Uh oh. Given that any constant type can get passed into a template, in any number, mixed with class parameters, KABLOOEY combinatorial explosion. Just to make things more difficult, what if any of T’s parameters are themselves templates?

  • 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-11T21:12:05+00:00Added an answer on May 11, 2026 at 9:12 pm

    boost::mpl does something like this (here’s their idea of binding an argument). However, you have to make a lot of assumptions to make it work, like using;

    template <class T> foo { }; typedef foo< int_<4> > my_foo_4;
    

    instead of

    template <int T> foo { }; typedef foo<4> my_foo_4;
    

    to not have to offer overloads for all int, char, bool, etc combination.

    I can’t think of anything that would be more effective than the boost::mpl approach, and in general, I would think any approach would suffer a lot of problems; a class template is NOT a type, and it can’t really be wedged into the type system that way (boost::mpl treats it as a function to create new types; more generically, it is used to create a “MetaFunction”). I’m not even sure if variadic templates are going to effect template template parameters (interesting question though).

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

Sidebar

Related Questions

This question sort of covers both ServerFault.com and here, but this seems more programming
I know this SO question, but it deals with the subject in more general
I am new to Python - be forewarned! I have a template class that
I'm asking this question with little/no knowledge of SMS gateways and it's relevant technologies,
I have asked a similar question to this but for GMail and I was
This PHP code... 207 if (getenv(HTTP_X_FORWARDED_FOR)) { 208 $ip = getenv('HTTP_X_FORWARD_FOR'); 209 $host =
.NET has a thing called remoting where you can pass objects around between separate
I am trying to create a macro that brings in the name of the
Consider this pseudo code: // an image is initialized UIImage *imagePX = [[UIImage alloc]initWithContentsOfFile:...
Question: I'm interested to know the best practice for killing a long standing operation

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.