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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:49:51+00:00 2026-05-25T16:49:51+00:00

I was under the (possibly incorrect) assumption that non-member functions in C++ do not

  • 0

I was under the (possibly incorrect) assumption that non-member functions in C++ do not dispatch based on the type of its arguments. But after reading about iterator_category it seems I can call a function depending on the category type of its argument and the call also handles inheritance. For instance if I write only the implementations for random access iterator and input iterator, all calls with a non-random access iterator will go to the function that accepts input iterator. This is a shortened example from the book

template <class T>
foo(T a) {
  foo(a, iterator_traits<T>::iterator_category());
}

template <class T>
foo(T a, random_access_iterator_tag) { \\body}

template <class T>
foo(T a, input_iterator_tag) { \\body}

// presumably this works even for
// ostream_iterator<int> i(cout);
// foo(i);

Is this kind of dispatch generally true or is this a special case ?

Is the compiler supposed to warn me if my implementations are not exhaustive, for example in the iterator category based example, if I gave an implementation for random access iterator and bidirectional iterator only, should the compiler complain that output iterator is not covered.

This is also the first time I encountered a function with an argument that is only a type, and not an object/instance. So can I define functions with built-in or user-defined types as one of its arguments without specifying the name of the instance/object of that type ?

The following seems to be an alternative to CRTP to achieve compile time polymorphism. Is that a correct interpretation

template<class T>
int foo(T a) {
  foo(a, a::type());
}

int foo(int a, base) { \\body }
int foo(int a, derived) { \\body }
  • 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-25T16:49:51+00:00Added an answer on May 25, 2026 at 4:49 pm

    You are indeed using compile-time polymorphism, which dispatches based on the static type of the object.

    The iterator categories are chained by inheritance (not the iterator themselves), so that:

    InputIterator <- ForwardIterator <- BidirectionalIterator <- RandomAccessIterator
    

    (should be an OutputIterator too, but it does not matter here)

    Using iterator_traits, you can retrieve the iterator category associated with the current iterator. You create a dummy value, and then the overload resolution process kicks in. Suppose for the sake of example that you have 3 overloads:

    template <class T>
    foo(T a, std::random_access_iterator_tag const&);
      // beware of slicing (in general)
    
    template <class T>
    foo(T a, std::forward_iterator_tag const&);
    
    template <class T>
    foo(T a, std::input_iterator_tag const&);
    

    Now suppose that I use foo with a list iterator:

    list<int> myList;
    foo(myList.begin());
    

    Then, the 4 foo are found in the scope (name resolution).

    • foo(T) is immediately discarded (wrong number of argument)
    • foo(T, std::random_access_iterator_tag const&) is discarded because there is no conversion from std::bidirectional_iterator_tag to std::random_access_iterator_tag.

    This leaves 2 foo both being compatible (note: if we had used an OutputIterator, we would not have anything left, and a compilation error would be raised at this point).

    We then finally get into the ranking part of the overload resolution process. Since std::forward_iterator_tag is a “closer” (more immediate) base than std::input_iterator_tag, it is therefore ranked higher.

    foo(T, std::forward_iterator_tag const&) is selected.


    Note the static portion of this though.

    std::forward_iterator_tag const& tag = std::vector<int>::iterator_category;
    foo(myVector.begin(), tag);
    

    Here, even though the tag really is (dynamic) a std::random_access_iterator_tag, it is seen by the system as a std::forward_iterator_tag, and thus the same overload that above will be selected.

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

Sidebar

Related Questions

What I'm talking about is: Is it possible that under certain circumstances the CPU
Is it possible to change the name(the one that apears under 'processes' in Task
Attached is a quick program written under interview conditions that is designed to flatten
Just curious. Using gcc/gdb under Ubuntu 9.10. Reading a C book that also often
This one's perhaps a duplicate of variable not recognized inside contentscript under the same
I was under the impression that an endpoint was defined in a config file
During development i have observed that certain PDAs with Windows CE and possibly Windows
I'm looking for a tool under windows or mac that allows me to monitor
I was always under the impression that when you're running a process as (domain\user)
I normally would create a limited rights user and run the process under that

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.