C++强制MeMyFun选择特定重载成员函数

C++强制MeMyFun选择特定重载成员函数,c++,member-function-pointers,mem-fun,C++,Member Function Pointers,Mem Fun,事实上,我已经想出了如何按照问题标题的建议去做,但不是以一种令人满意和便于携带的方式。让我更具体一点 这是我的代码的精简和修改版本: #include <algorithm> #include <functional> class A { public: int my_val() const { return _val; }; int& my_val() { throw "Can't do this"; }; // My cla

事实上,我已经想出了如何按照问题标题的建议去做,但不是以一种令人满意和便于携带的方式。让我更具体一点

这是我的代码的精简和修改版本:

#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;
}

现在,我的问题是,这个代码在Windows 7中用微软Visual Studio C++ 2008编译,并且在G+++版本4.1.2 20080704中不在Red Hat Linux中运行,在这里我得到以下错误:

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]
在linux中,如果我将mem\u-fun调用替换为:mem\u-fun-static\u-cast&A::my\u-val,那么它可以编译并正常工作。然而,我发现这个解决方案在美学上不如第一个。有没有另一种便携方式来做我想做的事情?也许有一个显而易见的简单方法可以做到这一点,而我只是对此大惊小怪

先谢谢你。
-曼纽尔

我不确定你的情况,但这会让我更高兴。定义您自己的功能:

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);
}
然后像这样使用它:

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;
}
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;
}
另一种避免演员阵容的方法是:

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;
}
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;
}

这是想法。

我认为,你最好把Boost绑定或STD::绑定,如果你有它或者只使用一个循环。@ 111111:Booo::Boost在这里是无济于事的,因为真正的问题是当你把表达式传递给一个能解决多个重载的绑定器时,C++除了语法外没有语法,指定要通过的重载。谢谢你,沃恩,我想这就是我要找的。请注意,您的代码是用g++编译的,但不是用MS Visual studio编译的,它抱怨错误C2914:“const_mem_fun”:无法推断模板参数,因为函数参数是不明确的,所以我必须指定const_mem_fun…我添加了另一种选择。它不是特别漂亮,但它确实避免了演员阵容,而且我认为它是可移植的。是的,你的选择是可移植的,事实上我一点也不觉得它难看。谢谢