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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:15:31+00:00 2026-06-14T23:15:31+00:00

I am stuck with this error . I have found a workaround too but

  • 0

I am stuck with this error . I have found a workaround too but it kind of kills the whole purpose of exercise.

I am trying to create a function which will take two iterators pointing to same container . I will find the sum of elements between them. I created general function for the sequential containers like vector which works fine. I overloaded the same function for associative containers. This is the one giving error.

map<string,double> myMap;
myMap["B"]=1.0;
myMap["C"]=2.0;
myMap["S"]=3.0;
myMap["G"]=4.0;
myMap["P"]=5.0;

map<string,double>::const_iterator iter1=myMap.begin();
map<string,double>::const_iterator iter2=myMap.end();

cout<<"\nSum of map using the iterator specified range is: "<<Sum(iter1,iter2)<<"\n"; 
//Above line giving error. Intellisense is saying: Sum, Error: no instance of overloaded function "Sum" matches the argument list.

//function to calculate the sum is listed below (It appears in a header file with <map> header included):
template <typename T1,typename T2>
double Sum(const typename std::map<T1,T2>::const_iterator& input_begin,const typename std::map<T1,T2>::const_iterator& input_end)
{
double finalSum=0;
typename std::map<T1,T2>::const_iterator iter=input_begin;

for(iter; iter!=input_end; ++iter)
{
    finalSum=finalSum+ (iter)->second;
}

return finalSum;
}

Compilation error is:
1>c:\documents and settings\ABC\my documents\visual studio 2010\projects\demo.cpp(41): error C2783: ‘double Sum(const std::map::const_iterator &,const std::map::const_iterator &)’ : could not deduce template argument for ‘T1’

Workaround:

If call Sum(iter1,iter2) is replaced with Sum < string,double > (iter1,iter2), it compiles fine.

Was I trying to do something impossible as per C++ standards in the first place?

  • 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-14T23:15:33+00:00Added an answer on June 14, 2026 at 11:15 pm

    The error is actually quite clear, in the following template:

    template <typename T1,typename T2>
    double Sum(const typename std::map<T1,T2>::const_iterator& input_begin,
               const typename std::map<T1,T2>::const_iterator& input_end)
    

    The types T1 and T2 cannot be deduced from the argument at the place of call. This is defined as such in the standard and if you think about it (in the general case) it makes sense.

    Consider that instead of std::map<>::const_iterator you had sometemplate<T>::nested_type and that the argument at the place of call was an int. If the compiler had to deduce the type, it would have to instantiate sometemplate for all possible types T in the universe (infinite set) and find for which of them the nested type nested_type is a typedef to int.

    As someone points out in the comments you can change the template so that instead of being templated on the key and value types of the map it just takes the iterators.


    Delegating extraction of the value

    This is a workaround to providing a single implementation of Sum that can deal with both sequential and associative containers.

    namespace detail {
    template <typename T> T valueOf( T const & t ) { return t; }
    template <typename K, typename V>
    V valueOf( std::pair<const K, V> const & p ) {
       return p.second;
    }
    }
    template <typename Iterator>
    double Sum( Iterator begin, Iterator end ) {
      double result = 0;
      for (; begin != end; ++begin) {
         result += detail::valueOf(*begin);
      }
      return result;
    }
    

    I have not tested the code, but that should do it. And this is probably much simpler than using SFINAE on the Sum template.

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

Sidebar

Related Questions

i am trying to create the following mysql procedure, but i am stuck for
I am trying to run GWT test from eclipse 3.6 but have stuck with
I am getting this error: integerMultiplication.rb:4:in `untMul': stack level too deep (SystemStackError) in this
Im stuck with this little project in C# but basically my problem is this:
I have been stuck on this for a while now and I cannot seem
So I have been stuck on this problem of sharing a function from one
I'm sure the answer is pretty simple, but I got stuck in this: Welcome
Ive been stuck on this all day and finally found a script which would
This is my first time using StackOverflow myself. I have found many answers to
I have this problem with Headers already sent with Magento. I get this error:

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.