Functions defined in <algorithm>, small concern..
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.
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.
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
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement