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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:15:07+00:00 2026-06-18T19:15:07+00:00

I have a template function, that needs to be specialized for iterators. So what

  • 0

I have a template function, that needs to be specialized for iterators. So what I did was along the lines of:

template <typename T>
void function2(T whatever, typename std::iterator_traits<T>::pointer) // ... iterator

template <typename T>
void function2(T whatever, ...) // ... non-iterator

template <typename T>
void function(T whatever) {
    function2(whatever, NULL);
}

And I’ve hit a wall, because Microsoft standard library specializes std::iterator_traits for all numeric types (bool, char, int, float…). And it does it so that reference and pointer are non-void, despite the fact that neither operator* nor operator-> can be called on those types.

Ok, I can checki the std::iterator_traits<T>::category derives std::input_iterator (actually I think std::forward_iterator is more appropriate in my case) at the cost of some more complex template machinery.

I would however be interested in knowing:

  • Why do they define iterator_traits for types, that don’t conform to the iterator concept (even output iterators need at least unary operator*, none of these types have one.
  • Are they violating C++ specification in doing so? Not that Microsoft wouldn’t be violating it all over the place, but if they are I would be satisfied with compiler-specific workaround, if they don’t obviously not.
  • And is it even workable in general anyway? It appears the std::iterator_traits<T>::pointer always exists, but is undefined and that leads to error rather than SFINAE.
  • 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-06-18T19:15:08+00:00Added an answer on June 18, 2026 at 7:15 pm

    Other functions [res.on.functions]

    […]

    2 In particular, the effects are undefined in the following cases:

    • for types used as template arguments when instantiating a template component, if the operations on
      the type do not implement the semantics of the applicable Requirements subclause

    In order to use iterator_traits<T>, T must be an iterator. If it isn’t, the behaviour is undefined. You cannot detect whether a type T is an iterator at compile time. It is not even theoretically possible, since a type is allowed to support the same operators and typedefs as iterators, so that an implementation’s generic iterator_traits<T> can be instantiated without any error or warning message, but with a completely different meaning.

    Thinking about it, since your comment clarifies that a reasonable guess is good enough, I think you’re best off using SFINAE and enable_if to detect core operations (unary * and prefix ++), using std::iterator_traits<T>::pointer only if those conditions are met.

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

Sidebar

Related Questions

I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
I have a template class that has a template member function that needs to
I have a template class with a variadic template member function that I am
Let's assume we have a template function foo: template<class T> void foo(T arg) {
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
Suppose I have a static function template template<int I> void ft() within a struct
I've been trying to implement a function that needs partial template specializations and fallen
I have a function that needs to enumerate an iterator multiple times, but according
In C++, I have a certain template function that, on a given condition, calls

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.