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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:37:54+00:00 2026-06-14T20:37:54+00:00

Quite frequently in C++11 I need to define a function that takes a container

  • 0

Quite frequently in C++11 I need to define a function that takes a container as a parameter.

For example lets define a function addup (yes just a simple version of std::accumulate):

template <class I>
int addup (I first, I last)
{
    int x = 0;
    while ( first != last )
        x += *first++;
    return x;
}

This takes an iterator range, which is flexible and the standard library idiom.

However suppose I have a function:

vector<T> f();

I have to do this:

auto v = f();
int x = addup(v.begin(), v.end());

I would rather just do this:

int x = addup(f());

Just like I can do this:

for (auto t : f())
    ...

In the spirit of range-based for I would like something like this:

template<class C>
int addup(C&& container)
{
    addup(beginexpr(container), endexpr(container)); // ???
}

In the standard it says in 6.5.4 (paraphrasing):

(A) if container is an array type, beginexpr and endexpr are container and container + bound, respectively, where bound is the array bound.

(B) if container is a class type, the unqualified-ids begin and end are looked up in the scope of class container as if by class member access lookup (3.4.5), and if either (or both) finds at least one declaration, beginexpr and endexpr are container.begin() and container.end(), respectively;

(C) otherwise, beginexpr and endexpr are begin(container) and end(container), respectively, where begin and end are looked up with argument-dependent lookup (3.4.2).

Is it possible to define a set of overloads or specializations of addup such that it will handle the four cases, and not conflict with other overloads? That is firstly a regular iterator pair function, and then each of A, B and C above. How?

(If this is possible than why doesn’t the standard library offer such overloads?)

Also, what if the function takes extra parameters beyond the container? Can we modify the overloads in such a way that an optional extra parameter x (one with a default value) added to all of them will not make the following two calls ambiguous:

addup(v.begin(), v.end());
addup(v, x);

That is can we statically assert (using "SFINAE" or similar) that the template parameter has to be an iterator, an array, a container class, etc – and have this information used for overload disambiguation?

  • 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-14T20:37:55+00:00Added an answer on June 14, 2026 at 8:37 pm

    This is what I would do:

    template<class Range>
    int addup(Range&& range)
    {
        using std::begin;
        using std::end;
        addup(begin(range), end(range));  // begin(), NOT std::begin()  (ADL)
    }
    

    It will handle all the important cases and does ADL properly. I’m not sure if it’s equivalent to what ranged-based-for does but in my opinion it is the best solution.

    the following two calls ambiguous:

    I haven’t compiled, but I don’t see any ambiguity there unless x needs an implicit conversion. You can also use boost::make_iterator_range and avoid having an iterator parameter overload.


    I think this will also work:

    template<class Range>
    int addup(Range&& range)
    {
        int x = 0;
        for(auto&& v : range)
            x += v;
        return x; 
    }
    
    template <class I>
    int addup (I first, I last)
    {
        return addup(boost::make_iterator_range(first, last));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that I use quite frequently, which allows me to write
I need to do several two-step or three-step commands in Git quite frequently, that
When discussing sealed classes, the term virtual function table is mentioned quite frequently. What
So, I have an application that communicates with a server quite frequently. I have
So I have a large table that I query (select only) quite frequently. The
I'm making use of an open source project that is changing quite frequently. It
I use cast() from the reshape package quite frequently. Almost every time, this warning
We use Infopath at work, quite frequently for form design and then we integrate
Quite often, I find myself wanting a simple, dump object in Python which behaves
A little background... I use Windows XP, Vista, and 7 quite frequently. As such,

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.