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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:27:08+00:00 2026-05-24T02:27:08+00:00

I am teaching myself template programming in C++, so some of my assumptions may

  • 0

I am teaching myself template programming in C++, so some of my assumptions may be wrong – please correct me if you see any errors.

I am trying to use an STL list as a template parameter to a function. The function is supposed to be used with all sorts of data types, so I have defined the function as template<class T> rather than template<template<> class T> in its original declaration. I now want to specialise it to support a template class.

template<class T> 
void function(T param)
{
  // do something with T
}

template<template <class T, class Allocator> class listPlaceholder> 
void function(std::list<T, Allocator> param)
{
  // do something with every element in param (I actually need to know it's a list)
  std::list<T, Allocator>::iterator current = param.begin();
  std::list<T, Allocator>::iterator end = param.end();

  do {
    function<T>(*current);
  } while (++current != end);
}

The problem is that when I try to compile this code (under GCC) it says that T and Allocator are not defined in the scope. My primary question is “how do I specialise for template classes?” and secondly, if it is possible, “how do I extract the template template parameters?”.

As mentioned previously, I am learning template programming, so obvious solutions are welcome.

  • 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-24T02:27:10+00:00Added an answer on May 24, 2026 at 2:27 am

    You want to declare those parameters

    template<template <class T, class Allocator> class listPlaceholder, 
             class T, class Allocator> 
    void function(listPlaceholder<T, Allocator> param)
    {
      // do something with every element in param (I actually need to know it's a list)
      typename listPlaceholder<T, Allocator>::iterator current = param.begin();
      typename listPlaceholder<T, Allocator>::iterator end = param.end();
    
      do {
        function<T>(*current);
      } while (++current != end);
    }
    

    The names you used in the formal parameter list have no meaning. You also forgot to use listPlaceholder actually. But I’m assuming that was accidental.

    As another poster said, you also need the typename keyword because the names are dependent names.

    For why the names in the formal list are meaningless, compare it to function pointers:

    void f(void (*p)(int t, int allocator), int t, int allocator) {
      p(t, allocator);
    }
    
    void g(int a, int b) { 
    }
    
    int main() {
      f(&g, 0, 1);
    }
    

    What is important is only the type of the parameters, and I could have written void(*p)(int, int) too. In your case, what is important is only that both parameters are type parameters. So you could have written the template template parameter also as template<class, class> class listPlaceholder too, completely equivalent.

    Last but not least, I would like to emphasize that you have not specialized function, but you have overloaded it with another template. So, both functions are two completely different function templates.

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

Sidebar

Related Questions

I've recently started teaching myself the standard template library. I was curious as to
I am teaching myself to use JavaCC in a hobby project, and have a
I've been teaching myself functional programming, and I'm currently writing different higher order functions
I'm still teaching myself how to bind and use observable collection. One problem that
I am teaching myself PowerShell by writing a simple parser. I use the .Net
I'm teaching myself XSLT and am still trying to wrap my brain around the
I've been teaching myself C#, and I'm just learning how to use custom data
I'm teaching myself techniques of programming with exception safety mode of ;) and I
I am teaching myself some Python and I have come across a problem which
I'm teaching myself some Django tonight using the local dev package at instantdjango.com I

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.