473,379 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Why can't one specify which overloaded function to use in transform (a, b, c, d)?

I have been working on an exercise in Accelerated C++ which shows how to use an auxiliary function to call an overloaded function when using transform.

grade is an overloaded function, so this won't work:

transform(students.begin(), students.end(), back_inserter(grades), grade);

So, as a workaround, we create an auxiliary function double grade_aux(const Student_info& s) that returns the particular overloaded function we want, grade(s). So now we have a transform call that works:

transform(students.begin(), students.end(), back_inserter(grade), grade_aux);

My question is, why couldn't we have saved a step and simply used:

transform(students.begin(), students.end(), back_insterter(grade), grade(s));

or something like that to specify which version of our overloaded function we want to call?
Nov 29 '12 #1

✓ answered by weaknessforcats

The C++ Standard Library has been designed rather than just coded. That means you need to observe all the library rules. In the case of transform<>, the last argument is either a unary-function or a binary-function. That is, there is an overload of transform<> in the lbrary.

A unary function is one that has one argument of first-argument-type and returns true or false. A binary function has two arguments and returns true or false. The arguments are first-argument-type and second-argument-type.

Therefore, any functions you write with these prototypes can be used with transforms<>.

The function is passed in by address using a function pointer so the compiler needs to know what kind of arguments it has in order to make a correct call. You are allowed only one of these two formats.

3 2055
weaknessforcats
9,208 Expert Mod 8TB
The C++ Standard Library has been designed rather than just coded. That means you need to observe all the library rules. In the case of transform<>, the last argument is either a unary-function or a binary-function. That is, there is an overload of transform<> in the lbrary.

A unary function is one that has one argument of first-argument-type and returns true or false. A binary function has two arguments and returns true or false. The arguments are first-argument-type and second-argument-type.

Therefore, any functions you write with these prototypes can be used with transforms<>.

The function is passed in by address using a function pointer so the compiler needs to know what kind of arguments it has in order to make a correct call. You are allowed only one of these two formats.
Nov 29 '12 #2
I think I understand how transform<> works now, but I don't really understand it from the design perspective. It still seems like one should be able to use:

transform(students.begin(), students.end(), back_inserter(grades), grade(s));

Isn't it clear I'm calling the unary-op version of transform<> because I am using just four parameters? And, by including the parameter (s) with grade, wouldn't this be a more efficient way to tell the compiler which version of grade I wanted to use?

Perhaps my question would be better stated: Why can't a function pointer point to an overloaded function? Or, why can't an algorithm be designed to recognize an overloaded function?


Here's the code to which I have been referring:

Expand|Select|Wrap|Line Numbers
  1. double median_analysis(const vector<Student_info>& students)
  2. {
  3.     vector<double> grades;
  4.  
  5.     transform(students.begin(), students.end(), back_inserter(grades), grade_aux);
  6.  
  7.     return median(grades);
  8. }
  9.  
Expand|Select|Wrap|Line Numbers
  1. double grade_aux(const Student_info& s)
  2. {
  3.     return grade(s);
  4. }
  5.  
Nov 30 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
grade_aux is supposed to be a function pointer. In this case it's a pointer to a function that takes a Student_info and returns a double.

So you just specify grade_aux in your transform<> .

You probably can't have overloads since you have only one argument and it has to be Student_info.

transform<> is expecting a pointer to a unary function with an argument of the type pointed at by your iterators.
Dec 1 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Roy Yao | last post by:
Why the following code let my compiler complain an overloaded function Init()? // code begin template<class T> class BicircularList { template<class T> class Iterator; template<class T> class...
14
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast ,...
6
by: PengYu.UT | last post by:
Hi, I run into error with the following program. Would you please help me? Best wishes, Peng struct tag1{}; struct tag2{};
4
by: Vish | last post by:
Hi all, I am having a build error in one of the overloaded functions in my class. The function takes either a string as a parameter or a type referenced in another dll as a parameter. My class...
1
by: siegfried | last post by:
The std::lower_bound function requires a function pointer as its last argument. This is simple if you don't overload. How do I I call std::lower_bound with the last argument a function pointer...
5
by: rolandz | last post by:
Hi, Maybe somebody has been fighting with the problem that I do, currently. I have a class that has method f(). The two versions of the f() method accept different objects: Int and Short. These...
6
by: c1t1z3n | last post by:
hiya, i'm having this weird error on my project. As far as I know "ambiguous call to overloaded function" should only occur when the compiler must choose from several methods, but here i don't think...
1
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; void print(char c) { cout << "from print(char c) : " << c << endl; return;
4
by: Noah Roberts | last post by:
I am trying to keep a vector of tuples sorted based on the first member and so for my insert function I am calling std::lower_bound with a bound comparator like the following: ...
4
by: Joseph Turian | last post by:
I have a templated class with the following methods: Vocab(const T& t); Vocab(unsigned uid); However, when T = unsigned, and I call Vocab(unsigned(0)) then the compiler rightly complains about...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.