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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:09:26+00:00 2026-05-17T15:09:26+00:00

To be consistent with other classes in a library, my array class below has

  • 0

To be consistent with other classes in a library, my array class below has two read() methods. The first reads the entire array to an output iterator and returns an error code, the second reads a single value and returns that (using exceptions for errors).

The problem I have is that if I call the second read(size_t idx) method with an int, the compiler prefers the templated method. I need to explicitly specify an unsigned int to make it work. My question is, what are my options around this problem:

  • Rename either read function to avoid overloading
  • Use something like boost::enable_if to prevent the iterator version from working with non-iterators. This sullies the interface though…
  • Any other ideas that I’m missing?

————————————————————-

#include <iostream>
#include <iterator>
#include <vector>

struct FooArray
{
  template <typename TIter>
  int read( TIter out )
  {    
    *out++ = 123.456; // copy stuff to output iterator
    return 99;        // error code
  }

  double read( size_t index )
  {    
    return 1.234; // return value at index
  }
};

int main(int argc, char**argv)
{
  FooArray tmp;
  std::cout << tmp.read(10u) << std::endl;
  /* std::cout << tmp.read(10) << std::endl;   COMPILER ERROR */
  tmp.read( std::ostream_iterator<double>(std::cout,"\n") ); 
}
  • 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-17T15:09:26+00:00Added an answer on May 17, 2026 at 3:09 pm

    Furthermore, I don’t think size_t is guaranteed to be a synonym for unsigned, so even with the “u” suffix the code might not be portable.

    std::string has a similar problem that it has to distinguish between:

    string s(10, 97);  //size_t, char
    string t(s.begin(), s.end()); //iter
    

    That would internally forward the call to a suitable helper function (needs a compiler-time test somewhere if the arguments are integral).

    However, in your case, the return types are different as well, therefore you’ll have to select the correct overload to begin with.

    It won’t look that bad with enable_if:

    #include <boost/utility/enable_if.hpp>
    #include <boost/type_traits/is_integral.hpp>
    
    struct FooArray
    {
      template <typename TIter>
      typename boost::disable_if<boost::is_integral<TIter>, int>::type
           read( TIter out )
      {
        *out++ = 123.456; // copy stuff to output iterator
        return 99;        // error code
      }
    
      double read( size_t index )
      {
        return 1.234; // return value at index
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something I'm not much consistent about and always curious about what other
I'd like a consistent and simple way to throw exceptions in JNI code; something
Does anyone have any advice for a consistent way to unit test a multithreaded
I'm looking for a consistent way to structure my use of formatting strings throughout
Is the return value of GetHashCode() guaranteed to be consistent assuming the same string
People have been developing own solutions to the following problems: Consistent messaging frameworks for
I need to be able to insert/update objects at a consistent rate of at
This question is related to How to make consistent dll binaries across VS versions
When creating web pages how do we achieve a consistent font size across browsers.
Are there any best practices (or even standards) to store addresses in a consistent

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.