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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:35:38+00:00 2026-05-26T18:35:38+00:00

I have a problem where I need to detect whether a given type is

  • 0

I have a problem where I need to detect whether a given type is an instance of a known nested type such as std::vector::iterator at compile time. I’d like to create the type trait is_std_vector_iterator:

#include <type_traits>
#include <vector>

template<typename T> struct is_std_vector_iterator : std::false_type {};

template<typename T, typename Allocator>
  struct is_std_vector_iterator<typename std::vector<T,Allocator>::iterator>
    : std::true_type
{};

int main()
{
  return 0;
}

But I receive the compiler error:

$ g++ -std=c++0x test.cpp 
test.cpp:7: error: template parameters not used in partial specialization:
test.cpp:7: error:         ‘T’
test.cpp:7: error:         ‘Allocator’

Is it possible to check for a dependent type like std::vector<T,Allocator>::iterator?


Here’s a motivating use case of such a trait:

template<typename Iterator>
Iterator my_copy(Iterator first, Iterator last, Iterator result, std::true_type)
{
  // iterators are just pointer wrappers; call memcpy
  memcpy(&*result, &*first, sizeof(typename Iterator::value_type) * last - first);
  return result + last - first;
}

template<typename Iterator>
Iterator my_copy(Iterator first, Iterator last, Iterator result, std::false_type)
{
  // use a general copy
  return std::copy(first, last, result);
}

template<typename Iterator>
Iterator my_copy(Iterator first, Iterator last, Iterator result)
{
  // dispatch based on the type of Iterator
  return my_copy(first, last, result, typename is_std_vector_iterator<Iterator1>::type())
}
  • 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-26T18:35:38+00:00Added an answer on May 26, 2026 at 6:35 pm

    Well, in the simplest case scenario it could look something like this:

    #include <type_traits>
    #include <vector>
    #include <list>
    #include <cstdio>
    
    template <typename T>
    typename std::enable_if<
        std::is_same<typename std::vector<typename T::value_type>::iterator, T>::value
        , void>::type
    do_something (T begin, T end)
    {
        std::printf ("Got vector iterators!\n");
    }
    
    template <typename T>
    typename std::enable_if<
        !std::is_same<typename std::vector<typename T::value_type>::iterator, T>::value
        , void>::type
    do_something (T begin, T end)
    {
        std::printf ("Got something other than vector iterators!\n");
    }
    
    template <typename T>
    typename std::enable_if<std::is_pod<T>::value, void>::type
    do_something (T begin, T end)
    {
        std::printf ("Got some POD iterators!\n");
    }
    
    int main()
    {
        std::vector<int> ivec;
        std::list<int> ilist;
        char cdata[64];
    
        do_something (ivec.begin (), ivec.end ());
        do_something (ilist.begin (), ilist.end ());
        do_something (&cdata[0], cdata + 32);
    
        return 0;
    }
    

    But the real problem comes when someone decides to use allocator different from default one. Since you want to check iterator against some well known type, not a well known template, then you can basically use this and possibly extend it with some allocators that you are aware of. Otherwise, a template instantiated with different types is a different type, and I am not sure if there is a way to test if a type is an instance of template specialized with some arbitrary parameter, there is probably no such way.

    On the other hand, you may solve this problem differently. For example, what difference it makes whether this is std::vector<...> iterator or not? It might make sense to check whether it is random access or not, etc.

    UPDATE:

    For continuously laid out memory, I’d say the best bet is to use iterator traits and check for random access tag. For example:

    #include <type_traits>
    #include <functional>
    #include <vector>
    #include <list>
    #include <cstdio>
    
    template <typename T>
    struct is_random_access_iterator : std::is_same<
        typename std::iterator_traits<T>::iterator_category
        , std::random_access_iterator_tag>
    {};
    
    template <typename T>
    typename std::enable_if<is_random_access_iterator<T>::value>::type
    do_something (T begin, T end)
    {
        std::printf ("Random access granted!\n");
    }
    
    template <typename T>
    typename std::enable_if<!is_random_access_iterator<T>::value>::type
    do_something (T begin, T end)
    {
        std::printf ("No random access for us today!\n");
    }
    
    int main()
    {
        std::vector<int> ivec;
        std::list<int> ilist;
        char cdata[32];
    
        do_something (ivec.begin (), ivec.end ());
        do_something (ilist.begin (), ilist.end ());
        do_something (&cdata[0], cdata + sizeof (cdata) / sizeof (cdata[0]));
    
        return 0;
    }
    

    This one will be definitely simpler and even more solid than checking against std::vector with allocators. However, even in this case someone can fool you if they really want, buy providing you random access iterator that provides seamless access to different chunks of memory, and you will have big problems once you convert that into a pointer use pointer arithmetics rather than iterator’s overloaded operators. You can protect yourself against that only by comparing memory addresses while changing both raw pointer and iterator, but there is no juice.

    Hope it helps.

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

Sidebar

Related Questions

I have a problem with reflection. I need to find the type that instantiates
I have a little problem to detect when the application is finished. I need
I have the following problem: We need to find the next august. I other
I have a problem where I need to remove all code and triggers from
I have the following problem: I need to use XSLFO to generate a 2-column
I have the next problem: I need to process only 1 request at a
I have a problem where I always need to give simple access to my
I have a problem. I need to host grid with controls in ScrollViewer to
I have a problem. I need to trace all read/write operations to the registry
I have a problem: imagine I have a plugin-based system. I need some kind

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.