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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:05:53+00:00 2026-06-10T15:05:53+00:00

I would like to write a constructor for MyClass that take an argument and

  • 0

I would like to write a constructor for MyClass that take an argument and I want this to compile only if the argument is a pointer or an iterator (something having iterator_traits). How to achieve this ?

  • 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-10T15:05:54+00:00Added an answer on June 10, 2026 at 3:05 pm

    Regrettably, there is no standard way to detect whether a class models Iterator. The simplest check would be that *it and ++it are both syntactically valid; you can do this using standard SFINAE techniques:

    template<typename T,
        typename = decltype(*std::declval<T&>(), void(), ++std::declval<T&>(), void())>
        MyClass(T);
    

    Considering the Iterator requirements from 24.2.2:2:

    template<typename T> typename std::enable_if<
        !std::is_void<decltype(*std::declval<T &>())>::value
        && std::is_same<decltype(++std::declval<T &>()),
                        typename std::add_lvalue_reference<T>::type>::value,
        std::true_type>::type has_iterator_requirements_helper(int);
    template<typename T> std::false_type has_iterator_requirements_helper(...);
    template<typename T> struct has_iterator_requirements:
        decltype(has_iterator_requirements_helper<T>(0)) {};
    
    template<typename, bool> struct is_iterator_check: std::false_type {};
    template<typename T> struct is_iterator_check<T, true>: std::true_type {
        typedef typename std::iterator_traits<T>::difference_type difference_type;
        typedef typename std::iterator_traits<T>::value_type value_type;
        typedef typename std::iterator_traits<T>::iterator_category iterator_category;
        typedef typename std::iterator_traits<T>::reference reference;
        typedef typename std::iterator_traits<T>::pointer pointer;
        static_assert(std::is_same<decltype(*std::declval<T &>()), reference>::value
            || std::is_void<reference>::value, "*r must be of type reference");
    };
    template<typename T> struct is_iterator: is_iterator_check<T,
        (std::is_pointer<T>::value
         && !std::is_void<typename std::remove_pointer<T>::type>::value
         && !std::is_function<typename std::remove_pointer<T>::type>::value
         ) || (std::is_copy_constructible<T>::value
         && std::is_copy_assignable<T>::value
         && std::is_nothrow_destructible<T>::value
         // TODO: check lvalues are swappable
         && has_iterator_requirements<T>::value
         )> {};
    

    The problem with trying to use iterator_traits is that it is a template defined for all types, and its instantiation will fail in a non-SFINAE context (recall that SFINAE only applies for direct substitution failure). libstdc++ has a conforming extension whereby instantiating iterator_traits on non-iterator types will produce an empty type; you can do a similar trick by checking for the existence of iterator_category on the type:

    template<typename T> std::true_type has_iterator_category_helper(
        T::iterator_category *);
    template<typename T> std::false_type has_iterator_category_helper(...);
    template<typename T> struct has_iterator_category<T>:
        decltype(has_iterator_category_helper<T>(0)) { };
    template<typename T> struct is_iterator: std::integral_constant<bool,
        std::is_pointer<T>::value || has_iterator_category<T>::value> {};
    
    template<typename T, typename = std::enable_if<is_iterator<T>::value>>
        MyClass(T);
    

    This will however not work for types that do not themselves expose iterator_category but have been adapted by a separate iterator_traits specialisation; in that case the simple SFINAE method makes more sense (and you can instantiate iterator_traits within the constructor to confirm that the type is iterator-like).

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

Sidebar

Related Questions

I would like to write an if statement that will do something base on
I would like to write a function that creates a plot of a set
I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that
I would like to write a cross-platform function in C++ that contains system calls.
I want to write something like the following: internal class InternalData { } public
I'd like to write a function that takes both a value constructor for a
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write a League Fixture generator in python, but I can't.
I would like to write to functional test for one of my rails 3

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.