4

I've actually figured out how to do what the title to my question suggests, but not in a satisfactory and portable way. Let me be more specific.

This is a stripped down and modified version of my code:

#include <algorithm>
#include <functional>

class A {
public:
    int  my_val() const { return _val; };
    int& my_val() { throw "Can't do this"; };
        // My class is actually derived from a super class which has both functions, but I don't want A to be able to access this second version
private:
    int _val;
}

std::vector<int> get_int_vector(const std::vector<A*>& a) {
    std::vector<int> b;
    b.reserve(a.size());
    transform( a.begin(), a.end(), inserter( b, b.end() ),
        std::mem_fun<int, const A>(&A::my_val) );
    return b;
}

Now, my issue is that this code compiles and works fine in Windows 7 with Microsoft Visual Studio C++ 2008, but not in Red Hat linux with g++ ( version 4.1.2 20080704 ), where I get the following error:

error: call of overloaded 'mem_fun(<unresolved overloaded function type>)' is ambiguous
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:713: note: candidates are: std::mem_fun_t<_Ret, _Tp> std::mem_fun(_Ret (_Tp::*)()) [with _Ret = int, _Tp = const A]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:718: note:                 std::const_mem_fun_t<_Ret, _Tp> std::mem_fun(_Ret (_Tp::*)()const) [with _Ret = int, _Tp = const A]

In linux it compiles and works fine if I replace the mem_fun() call with this: mem_fun( static_cast<int (A::*)() const>(&A::my_val) ). However I find this solution less aesthetically pleasing than the first one. Is there another portable way to do what I want? (Perhaps there's an obvious simple way to do this and I'm just making a big fuss over it...)

Thank you in advance. -Manuel

MPortilheiro
  • 128
  • 8

2 Answers2

1

I'm not sure about you, but this would be more pleasing to me. Define your own function:

template <typename S,typename T>
inline std::const_mem_fun_t<S,T> const_mem_fun(S (T::*f)() const)
{
  return std::const_mem_fun_t<S,T>(f);
}

and use it like this:

std::vector<int> get_int_vector(const std::vector<A*>& a) {
    std::vector<int> b;
    b.reserve(a.size());
    transform( a.begin(), a.end(), inserter( b, b.end() ),
        const_mem_fun(&A::my_val) );
    return b;
}

Another alternative to avoid the cast would be something like this:

std::vector<int> get_int_vector(const std::vector<A*>& a) {
    std::vector<int> b;
    b.reserve(a.size());
    int& (A::*my_val)() const = &A::my_val;
    transform( a.begin(), a.end(), inserter( b, b.end() ), std::mem_fun(my_val) );
    return b;
}
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • 1
    I think that this point you are better off bringing in boost bind (or std::bind if you have it) or just use a loop. – 111111 Mar 05 '12 at 16:18
  • @111111: boost::bind won't help here, as the true problem is that when you pass an expression to a binder that can resolve to multiple overloads, C++ has no syntax, other than to cast, to specify which overload you wanted to pass. – PlasmaHH Mar 05 '12 at 16:32
  • Thank you Vaughn, I think this is what I was looking for. Just one note, your code compiles with g++ but not with MS Visual studio, which complains "error C2914: 'const_mem_fun' : cannot deduce template argument as function argument is ambiguous", so I had to specify `const_mem_fun(...)`. – MPortilheiro Mar 05 '12 at 16:34
  • I've added another alternative. It isn't particularly pretty, but it does avoid the cast, and I think it will be portable. – Vaughn Cato Mar 05 '12 at 17:15
  • Yes, your alternative is portable, and in fact I don't find it ugly at all. Thanks! – MPortilheiro Mar 06 '12 at 14:19
0
typedef int (A::*MethodType)() const;
const_mem_fun(MethodType(&A::my_val));

This is the idea.

Basilevs
  • 22,440
  • 15
  • 57
  • 102