Create a template function for map<T1,T2> iterators

579 Views Asked by At

So I am trying to make 4 template functions that do the following: sum a generic container, sum a map, sum generic container iterators, and sum generic map iterators. I have managed to do the first three, but I can't figure out the sum map iterators. Here is my code:

Sum.hpp

#ifndef SUM_HPP
#define SUM_HPP

#include <iostream>
#include <map>
#include <string>
using namespace std;

//Sum Generic Container
template <typename T>
double Sum(const T& cont) {
    double sum = 0; 

    typename T::const_iterator iter = cont.begin();
    for (iter; iter != cont.end(); iter++) {
        sum += *iter;
    }

return sum;
}

//Sum Generic Map
template <typename T1, typename T2>
double Sum(const map<T1, T2>& mp) {
    double sum = 0;

    typename map<T1,T2>::const_iterator iter = mp.begin();
    for (iter; iter != mp.end(); iter++) {
        sum += iter->second;
    }

    return sum;
}

//Sum Generic Container Iterators
template <typename T>
double Sum(T& begin, const T& end) {
    double sum = 0;

    for (begin; begin != end; begin++) {
        sum += *begin;
    }

    return sum;
}

//Sum Generic Map Iterators
template <typename T1, typename T2>
double Sum(map<T1, T2>::iterator& begin, map<T1, T2>::iterator& end) {
    double sum = 0;

    for (begin; begin != end; begin++) {
        sum += begin->second;
    }

    return sum;
}


#endif  

Test.cpp:

#include "Sum.hpp"
#include <iostream>
#include <map>
using namespace std;

int main() {
    //Map
    cout << "map" << endl;
    map<string, double> mp;

    mp["weight"] = 5.5;
    mp["height"] = 6.7;
    mp["length"] = 8.4;

    map<string, double>::iterator mp_iter_begin = mp.begin();
    map<string, double>::iterator mp_iter_end = mp.end();
    cout << Sum(mp_iter_begin, mp_iter_end) << endl;

    return 0;
}

When I run the Sum() function it tries to call the Sum Generic Container Iterators function, if I comment out the Sum Generic Container Iterators function, I get a "no instance of overloaded function" error. Can anyone spot what I am doing incorrectly?

2

There are 2 best solutions below

6
EliSquared On BEST ANSWER

I figured it out how to do it the way I wanted to with specialization:

//Sum Generic Container Iterators
template <typename T>
double Sum(T& begin, const T& end) {
    double sum = 0;

    for (begin; begin != end; begin++) {
        sum += *begin;
    }

    return sum;
}

//Sum Map<string, double> Iterators
template <>
double Sum(map<string, double>::iterator& begin, const map<string, double>::iterator& end) {
    double sum = 0;

    for (begin; begin != end; begin++) {
        sum += begin->second;
    }

    return sum;
}

Note the importance of making sure the generic container iterator and map iterator function having matching arguments (both arguments are references and the second argument is a const for both template functions) or the specialization for map<string,double> will not work.

3
leslie.yao On

This belongs to non-deduced contexts:

1) The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id:

That means for map<T1, T2>::iterator, the template parameter T1 and T2 can't be deduced.

You can solve the issue via SFINAE, e.g.

//Sum Generic Container Iterators
template <typename T>
auto Sum(T begin, T end) -> remove_reference_t<decltype(*begin += *begin)> {

    remove_reference_t<decltype(*begin)> sum = 0;

    for (; begin != end; begin++) {
        sum += *begin;
    }

    return sum;
}

//Sum Generic Map Iterators
template <typename T>
auto Sum(T begin, T end) -> remove_reference_t<decltype(begin->second += begin->second)> {

    remove_reference_t<decltype(begin->second)> sum = 0;

    for (; begin != end; begin++) {
        sum += begin->second;
    }

    return sum;
}

LIVE