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

The Archive Base Latest Questions

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

I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly

  • 0

I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly for all types using the same syntax. Imagine we have the following:

template <typename T>
struct value_type {
  typedef T type;
};

#define VALUE_TYPE(T) typename value_type<T >::type

This will work for POD types. I can specialize it for my own class:

struct MyClass {
  typedef float value_type;
};

template <>
struct value_type<MyClass> {
  typedef MyClass::value_type type;
};

though I would prefer to avoid extra value_type instantiations in an ideal world.

The problem is with STL iterators. I need a specialization that gets me to the iterator hierarchy. This fails because the compiler chooses the base case:

template <>
struct value_type<std::_Iterator_base_aux> {  // MSVC implementation
  typedef value_type type;
};

Choosing a class higher up the hierarchy (_Iterator_with_base would be most natural because that is where value_type is defined) fails because it requires specifying all the iterator traits as template arguments.

Is what I’m trying to do even possible in C++?

  • 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-13T18:22:51+00:00Added an answer on May 13, 2026 at 6:22 pm

    You can use SFINAE to detect the presence of the value_type typedef. No need to specialize for individual types (which might not be possible, since you’d be relying entirely on internal implementation details).

    #include <vector>
    
    template <class T>
    struct has_value_type
    {
        typedef char true_type;
        typedef char false_type[2];
    
        //template not available if there's no nested value_type in U's scope
        template <class U>
        static true_type test(typename U::value_type* ); 
    
        //fallback
        template <class U>
        static false_type& test(...);
    
        //tests which overload of test is chosen for T
        static const bool value = sizeof(test<T>(0)) == sizeof(true_type);
    };
    
    template <class T, bool b>
    struct value_type_impl;
    
    template <class T>
    struct value_type_impl<T, false> //if T doesn't define value_type
    {
        typedef T type;
    };
    
    template <class T>
    struct value_type_impl<T, true> //if T defines value_type
    {
        typedef typename T::value_type type;
    };
    
    template <class T>
    struct value_type: value_type_impl<T, has_value_type<T>::value>
    {
    };
    
    struct MyClass {
      typedef float value_type;
    };
    
    template <class T>
    int foo(T )
    {
        return typename value_type<T>::type();
    }
    
    int main()
    {
        foo(MyClass());
        std::vector<int> vec;
        foo(vec.begin());
        foo(10);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 346k
  • Answers 346k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As long as it involves a simple sub-directory like my… May 14, 2026 at 6:09 am
  • Editorial Team
    Editorial Team added an answer You find it here https://addons.mozilla.org/en-US/firefox/addon/60 When you have installed the… May 14, 2026 at 6:09 am
  • Editorial Team
    Editorial Team added an answer Are you sure you need to create a new URI… May 14, 2026 at 6:09 am

Related Questions

I have class foo that contains a std::auto_ptr member that I would like to
Hey there, I have read the few posts here on when/how to use the
Does ADO.NET Entity Framework have a construct similar to LINQ to SQL's GetTable? I'm
I'd like the community's take on some thoughts I've had about Linq to Sql
I have a custom built application framework written in PHP which I have been

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.