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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:10:29+00:00 2026-05-15T20:10:29+00:00

I have to implement a function that takes an iterator. The iterator must dereference

  • 0

I have to implement a function that takes an iterator. The iterator must dereference to a certain type, say int:

template<typename iter>
  void f(iter i) {
  // do something here ...
  int t = *i;
  // do something here ...
}

The problem with this code is that if a user calls the function like this

vector<string> v;
v.push_back("aaa");
f(v.begin());

he will see an error pointing to some place in my code, not in his code (which will be confusing to him). I want the error to be in user’s code to ease debugging.

  • 1 1 Answer
  • 3 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-15T20:10:30+00:00Added an answer on May 15, 2026 at 8:10 pm

    GMan already pointed to a method to solve this via compile time assertions. There is another way to do this, which I prefer (it’s my favorite C++ technique). You can put constraints on function arguments in a way that the function is ignored for overload resolution if the constraints don’t fit. This is quite terrific, because you can fine tune your function overloads to arbitrary conditions. Here’s how:

    #include <boost/utility.hpp>
    #include <boost/type_traits.hpp>
    #include <vector>
    
    
    template<typename Iter> typename
    boost::enable_if<
        boost::is_same<typename Iter::value_type,int>,
    void>::type
    foo(Iter it) { }
    
    int main() {    
        std::vector<int> v; // this is OK
        foo(v.begin());
        std::vector<double> v2; // this is an error
        foo(v2.begin()); }
    

    If you compile this, you will get

    b.cc: In function 'int main()':
    b.cc:19:16: error: no matching function for call to 'foo(std::vector<double>::iterator)'
    

    This is because the compiler would consider foo() only, if it’s argument has a value_type type inside, which is ‘int’ (This is what the enable_if part means). The second call of foo() can’t satisfy this constraint.

    enable_if is mentioned a couple of times in SO, just search for it: https://stackoverflow.com/search?q=enable_if

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

Sidebar

Related Questions

Let's say I have a function that takes a list of function-list pairs, and
I have a function that takes a variable number of arguments in C that
I would like to implement a function that takes as input a size n
I have a simple function that takes a char and return a string, in
Hey guys, I'm currently trying to implement a function using C that takes in
I have a class template Foo<T> . I'd like to implement a non-member function
Hi guys I'm implementing an F# function that takes two lists of type :
I have to implement a function to get headers only (without doing a GET
I have an abstract base class and want to implement a function in the
I have a Linked List and I want to implement a function: Random_Shuffle_List (struct

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.