#### Archived

This topic is now archived and is closed to further replies.

# Functions defined in <algorithm>, small concern..

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Lets say I''m using an arbitrary function such as: transform(container.begin(), container.end(), back_inserter(container2), somefunction) Now if ''somefunction'' is an overloaded function, the compiler will have problems determining which one is being referenced since somefunction is not passing arguments. I know an auxiliary function can be used with a unique name where the compiler will face no ambiguity when calling it and it would solve the problem. The problem is that I don''t really understand why this is...I mean ''somefunction'' may have a version that would take a type that matches an element from container that transform() passes and I would think the compiler should be able to sort that out...Perhaps (if I haven''t confused anyone) someone can shed some light on this for me? Thanks guys.

##### Share on other sites
The C++ overloaded function resolution mechanism should choose the correct function to be called, but if you wanted to specify which overloaded function to be called, the best way to go about it would be to ditch your functions and use specialized functors.

template<typename T>class functor{public:T operator ()(const T& x, const T& y){return x*y;}};//specialize the class for an inttemplate<>class functor<int>{public:int operator () ( const int& x, const int& y){return x*x*x*x*y;}};

Something a little like that.

You can then pass the int functor to your transform algorithm

transform(...,functor<int>());

this would cause the int version of your functor to be called. You could do the same thing with any other type, and the correct one will be called. Hope that helps

##### Share on other sites
quote:
Original post by Nervo
Lets say I''m using an arbitrary function such as:

transform(container.begin(), container.end(), back_inserter(container2), somefunction)

Now if ''somefunction'' is an overloaded function, the compiler will have problems determining which one is being referenced since somefunction is not passing arguments. I know an auxiliary function can be used with a unique name where the compiler will face no ambiguity when calling it and it would solve the problem.

The problem is that I don''t really understand why this is...I mean ''somefunction'' may have a version that would take a type that matches an element from container that transform() passes and I would think the compiler should be able to sort that out...Perhaps (if I haven''t confused anyone) someone can shed some light on this for me? Thanks guys.

This functional aspect of C++ was bolted onto the imperative language, so this ambiguity is a result of that. A functional language would not have any difficulty picking the correct function, but C++ needs to know at the point of function invocation (as opposed to the point of use inside of the called function); if there is such ambiguity you have to tell it which one to use - with an explicit function pointer or through a custom functor.

1. 1
2. 2
3. 3
4. 4
Rutin
21
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632936
• Total Posts
3009313
• ### Who's Online (See full list)

There are no registered users currently online

×