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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:05:30+00:00 2026-05-26T14:05:30+00:00

I want to have a function with interface like this: template<typename T, typename R>

  • 0

I want to have a function with interface like this:

template<typename T, typename R> int find_index (const T& list, const R& value);

As I know, there is find() in STL that returns iterator. I need to return index of iterator (even for non-indexed containers such as std::list). I tried this code:

template<typename T, typename R>
int find_index (const T& list, const R& value)
{
    int index = 0;
    for (T::const_iterator it = list.begin(); it != list.end(); it++, index++)
        if ((*it) == value)
            return index;
    return -1;
}

But compiler shows error on it – seems like it is not allowed to get const_iterator from templated typename. Can I go around it?

At the worst case I can pass begin and end iterators to find_index arguments, but it looks not so fine. Would be thankful for elegant solution.

  • 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-26T14:05:31+00:00Added an answer on May 26, 2026 at 2:05 pm
    for (typename T::const_iterator it = list.begin(); it != list.end(); ++it, ++index)
    

    should solve your problem.

    When using dependent types (types depending on template parameters), the compiler does not know that const_iterator is a type until it instantiates the template with a concrete type, it could also just be a static variable or whatever. Using the typename keyword, you tell him that const_iterator is really a type.

    In C++11 you can also circumvent the whole typename issue using the auto keyword:

    for (auto it = list.begin(); it != list.end(); ++it, ++index)
    

    If you already have the iterator (maybe from some other operation), you can also just compute the distance from the list’s begin to this iterator:

    #include <iterator>
    
    int index = std::distance(list.begin(), it);
    

    But since this has linear complexity for a std::list, using your self-made find_index function is a better idea than std::find followed by std::distance, at least performance-wise.

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

Sidebar

Related Questions

I have some classes like this: interface class IA { }; interface class IB
I want to create a generic function with signature like this : void funcName<T>()
I have one problem I want to call a function and use the value
So I have a FileReader class, it looks like this: #define DISALLOW_COPY(type) \ type(const
I want to have a function which searches for a key in a collection
function focus(element) { document.form.element.focus(); } I want to have a function to focus on
I have function getCartItems in cart.js and I want to call that function in
I want to have a jquery function that will be activated when any checkbox
I want to have a generic function for a few types such as long,
I want to have one callback function after actions are done, I'm trying something

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.