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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:03:55+00:00 2026-06-17T21:03:55+00:00

I have a function where ‘T’ can either be a queue or a stack.

  • 0

I have a function where ‘T’ can either be a queue or a stack.
Currently for the function signature I have:

template <class T>
void foo(T<string> baa){

  //do something
}

Within foo I want to call T.pop() but I cannot do so.

I then wrote two version of pop, one for a queue and one for a stack.

string bar(stack<string> & baa){
  string t=baa.top();
  baa.pop();
  return t;
}
string bar(queue<string> & baa){
  string t=baa.front();
  baa.pop();
  return t;
}

I then tried to do this but it wouldn’t work. How am I supposed to do this?

template <class T>
void foo(T<string> baa){
  //do something
  string baz=bar(baa);
}

EDIT:I forgot that top() simply removes the top element. I have now edited the code fragment to reflect those changes. However it still isn’t working.

  • 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-17T21:03:56+00:00Added an answer on June 17, 2026 at 9:03 pm

    If you really want to do something like this, then syntactically what you need is template template parameters:

    template<template<typename> class T>
    void foo(T<string>& baa)
    {
        ...
    }
    

    However, be aware that the template parameters of the template template parameter T must match exactly the ones of the template template argument, even though those parameters have default argument values.

    In other words, since STL’s stack and queue container adapters accept two type parameters (even though the second one has a default argument value), if you want them to be deduced as the template template arguments of your function template foo(), the template template parameter T must take two type parameters:

    template<template<typename, typename> class T, typename C>
    void foo(T<string, C>& baa)
    {
        ...
    }
    

    I shall point out, though, that your current needs do not seem to require such a design. This is enough:

    template<typename T>
    string bar(T& baa)
    {
        string t = baa.front();
        baa.pop();
        return t;
    }
    

    You would be forced to adopt the solution with template template parameters only if you had other function template overloads of bar() that need to work on non-container types, but again this does not seem to be your case.

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

Sidebar

Related Questions

I have function send mail and it looks like this private void email(String emailTo,
I have function whose signature is something like so: void someFunc(ifstream ifile) { This
I have function along these lines: public void view(string msg) { messagebox.show(msg); } .
I have function which takes in an parameter of a class called Triple, and
This is driving me crazy: I have function void gst_init(int *argc, char **argv[]); in
I have function function bgSetDisableOverlappedContent(CAA: BOOL; var ErrorCode: DWORD; ErrorText: string): Boolean; begin errorCode
I have function which is removing and adding class into input tag, i want
I have function as below: foo :: Int -> a -> [a] foo n
I have function in my database class that returns the id of the people
I have function like this in my Bean: public String uploadFile(UploadedFile uploadedFile) { logger.info(Enter:

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.