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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:15:56+00:00 2026-05-23T08:15:56+00:00

I want to build a Stack class so the user will be able to

  • 0

I want to build a Stack class so the user will be able to choose which container he wants to use for the implementation of the Stack. For example, List/Vector.

Partial code:

stack.h

#ifndef STACK_H_
#define STACK_H_

template <typename T, template<typename T> class ContainerType>
class Stack{
    ContainerType<T> container;
public:
    Stack() : container(ContainerType<T>()){}

};

#endif /* STACK_H_ */

test.cpp

#include "stack.h"
#include <vector>

int main(){   
    Stack<int, std::vector<int> > stack;
    return 0;
}

Well, it doesn’t compile. I get the next errors on line:

Stack<int, std::vector<int> > stack;

Errors:

expected a class template, got `std::vector<int, std::allocator<int> >' test.cpp

invalid type in declaration before ';' token test.cpp

type/value mismatch at argument 2 in template parameter 
list for `template<class T, template<class T> class ContainerType> 
class Stack' test.cpp

‪
  • 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-23T08:15:57+00:00Added an answer on May 23, 2026 at 8:15 am

    First, it would be std::vector, and nothing else, because vector resides in the std namespace and you are asking for a template template parameter, while std::vector<int> is not a template anymore. Next, a std::vector actually takes two template parameters, one for the type and the other for the allocator:

    template <
        typename T,
        template<typename, typename> class ContainerType,
        typename Alloc = std::allocator<T>
    >
    class Stack{
      ContainerType<T, Alloc> container;
      // ...
    };
    
    // usage:
    Stack<int, std::vector> s;
    

    Now, this only enables containers with two template parameters as the underlying type, so you’re better off with what the standard does: take it as a normal type:

    template <typename T, typename ContainerType>
    class Stack{
      ContainerType container;
      // ...
    };
    
    // usage:
    Stack<int, std::vector<int> > s;
    

    To ensure that the underlying type has the same T, you can do a fake “static assert”, or if you have a C++0x enabled compiler, you can do an actual static assert:

    #include <tr1/type_traits> // C++03 us std::tr1::is_same
    //#include <type_traits> // C++0x, use std::is_same
    
    template <typename T, typename ContainerType>
    class Stack{
      typedef typename ContainerType::value_type underlying_value_type;
      typedef char ERROR_different_value_type[
                   std::tr1::is_same<T, underlying_value_type>::value ? 1 : -1
                                             ]
      ContainerType container;
      // ...
    };
    

    This works because if T is different from the used container’s T, it will be a typedef char ERROR_different_vale_type[-1], and an array of negative size can’t possibly exist, which causes a compiler error. 🙂 Now, with C++0x you can just static_assert that:

    #include <tr1/type_traits> // C++03
    //#include <type_traits> // C++0x
    
    template <typename T, typename ContainerType>
    class Stack{
      typedef typename ContainerType::value_type underlying_value_type;
      static_assert(std::tr1::is_same<T, underlying_value_type>::value,
        "Error: The type of the stack must be the same as the type of the container");
      ContainerType container;
      // ...
    };
    

    For convenience, you can now specify a default template argument for the common case:

    template <typename T, typename ContainerType = std::vector<T>>
    class Stack{
      ContainerType container;
      // ...
    };
    
    // usage:
    Stack<int> s;
    

    And at this point you can just use std::stack which does exactly this (though it uses std::deque as the underlying type). 🙂

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

Sidebar

Related Questions

I want to build a stack of resources that will be used by different
I want to build a lightweight linux configuration to use for development. The first
I want to build a site where the user can enter text and format
I have a generic abstract UserControl class, SensorControl , which I want all my
i want build a photography app with effects . e.g. old images with brown
I want build a sketch pad app on iPhone, I assume that this type
I want to build a bot that asks someone a few simple questions and
I want to build my own custom log4j (network) adapter to solve my problem
I want to build an executable to distribute to people without python installed on
I want to build two-dimentional array of strings where length of one dimention is

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.