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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:22:50+00:00 2026-05-23T20:22:50+00:00

refering to this answer , the second code block. My question is: If I

  • 0

refering to this answer, the second code block. My question is:

If I know I will be processing only std::lists<int>, and only <int>.
Is there a way to write this (the second block and the third one) without using templates and without passing the list by reference as suggested in the comments? Could you show it to me?

I think it makes sense to avoid using templates if the implementation only covers one single type right??? ( or am I too lazy? )

  • 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-23T20:22:51+00:00Added an answer on May 23, 2026 at 8:22 pm

    That’s my answer!

    You can pass a std::back_insert_iterator<std::list<int> > to a function with or without templates. Secretly, you’re still passing the list by reference, since the iterator itself holds a reference or pointer to the container.

    typedef std::back_insert_iterator<std::list<int> > OutputIterator;
    
    void getInts(OutputIterator out) {
        for (int i = 0; i < 10; ++i) {
            *(out++) = i;
        }
    }
    

    Then the caller does:

    std::list<int> l;
    getInts(std::back_inserter(l));
    

    You’re still “using templates” in the sense that back_inserter is a function template, and back_insert_iterator is a class template, but then again so is list. This way you aren’t writing any templates of your own.

    I disagree that it necessarily makes sense to avoid templates if you’re only interested in one type — a side-effect of C++ templates is type inference via template argument deduction, meaning that you don’t have to write out ridiculous types like std::back_insert_iterator<std::list<int> >, even once in a typedef. As you can see from the code, avoiding templates isn’t laziness, it’s actually less text to write the template precisely because you don’t have to mention the types. But if for some reason you want to go out of your way to restrict that function to only work with lists, and only with appending to the back of them, then you can go ahead.

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

Sidebar

Related Questions

I have a feeling I may already know the answer to this question, but
I know this is probably a first year question, but I'm having some problems
int i,j; std::string s; std::cin>>i>>j>>s>>s>>i; std::cout<<i<< <<j<< <<s<< <<i; Question Referring to the sample
Refering to a previously asked question , I would like to know how to
My apologies for the extremely basic question, I think I know the answer but
I have a question very similar to this one but the answer does not
This question has been asked before - but with no satisfying answer at all!
I'm referring to distinctions such as in this answer : ...bash isn't for writing
I have tried to do this refering to following link. http://skypher.com/index.php/2008/07/28/function-list-for-php/ But no success.
I get this, clicking 'no' means the page displays flawlessly. it's refering to this

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.