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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:16:15+00:00 2026-05-11T01:16:15+00:00

I need a template like this, which work perfectly template <typename container> void mySuperTempalte

  • 0

I need a template like this, which work perfectly

template <typename container> void mySuperTempalte (const container myCont) {     //do something here } 

then i want to specialize the above template for std::string so i came up with

template <typename container> void mySuperTempalte (const container<std::string> myCont) {     //check type of container     //do something here } 

which doesnt’t work, and throws an error. I would like to make the second example work and then IF possible i would like to add some code in the template to check if a std::vector/std::deque/std::list was used, to do something differently in each case. So i used templates because 99% of the code is the same for both vectors and deques etc.

  • 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. 2026-05-11T01:16:15+00:00Added an answer on May 11, 2026 at 1:16 am

    If I am understanding your problem correctly you have an algorithm that will work for STL containers vector, deque etc but are trying to write a template specialisation for string. If this is the case then you can write the generalised templated method that you defined in your question:-

    template<typename container> void mySuperTempalte( const container &myCont ) {     // Implement STL container code } 

    Then for your string specialisation you declare:-

    template<> void mySuperTempalte( const container<std::string> &myCont ) {     // Implement the string code } 

    For any other specialisation just change the type declaration for myCont. If you really need to do this for the vector and deque containers then make the template parameter the parameter for the type in that container rather than the container itself as Sep suggested.

    template<typename C> void mySuperTempalte( const std::vector<C> &myCont) {     // check type of container     // do something here } 

    It’s worth trying to avoid this by making your first implementation work with all STL containers to make your life easier, then you only need the specialisation for the string class. Even consider converting your string to a vector to avoid the specialisation all together.

    On a side note, I’ve changed the container parameter to a const reference, I assume this is what you want, as you declare the object const anyway, this way you avoid a copy.

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

Sidebar

Related Questions

I have a dictionary with embedded objects, which looks something like this: notes =
I need to do something like {{ article.product.images.first.image.url }} In my template, but there
The workflow is like this: we download a template form, prefill values which will
I have passed Python list to template like l=['text','there is no salary','I need alcohol','and
I need very basic template engine, which is incapsulated in single class. So I
I need a link in django template which turns into logout if user is
Elsewhere on the Web, you can find recommendations on using something like this to
I cannot figure this out. I need to have an abstract template base class,
I have a models in Django that are something like this: class Classification(models.Model): name
I have trouble when designing classes like this class C1 { public: void foo();

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.