c++ - typedef 以下类型 : Pointer to a member function Fof "any" class having a member function F

标签 c++ templates operators pointer-to-member

我实际上正在尝试定义以下类型:X 和 Y 是类型,我想定义指向“a”类 T 的成员函数 Y F( X ) const 的指针的模板类型。

为此,我定义了以下内容:

template<typename T, typename X, typename Y>
struct GenericFunc
{
    typedef Y (T::*F) ( X ) const ;
};

现在如果

class Fct
{
    public:
        Fct( /* something */);
        double F( double ) const
        {
            // stuff...
        }
};

如果我声明

Fct thef(/* something */);
GenericFunc<Fct, double, double>::F phi ;

我想用 thef 和 phi 得到一个 double,但我不能成功。任何想法 ?什么是 F 是 operator() ?

事实上,我可以这样做:

Fct thef(/* something */);
double (Fct::*fptr) (double) const = & Fct::F ; // or double (Fct::*fptr) (double) const = & Fct::operator() ;
double x = 3.14159 ;
double y = (thef.*fptr)(x);

但我想以泛型的方式来做,也就是说,在具有成员函数 F 的类 T 上,在 F 的参数的类型 X 上,以及在 F 的返回类型 Y 上泛型。

非常感谢

PS:我使用的是 c++ < c++11,这就是我在结构中使用 typedef 的原因,我想坚持这一点。

最佳答案

这个有效:

Fct thef;
GenericFunc<Fct, double, double>::F phi = &Fct::F;
double x = (thef.*phi)(1);

Online Demo

关于c++ - typedef 以下类型 : Pointer to a member function Fof "any" class having a member function F,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25900745/

相关文章:

c++ - istringstream、ostringstream 和 stringstream 有什么区别?/为什么不在每种情况下都使用 stringstream?

c++ - #include 混淆和类

html - Angular.js 模板到纯 html

c# - 为什么模运算符 (%) 结果在 C# 中隐式转换到左侧而不是右侧?

php - ASP 中是否有用于附加到字符串变量的运算符?

java - double 和 '^' 运算符,不可能吗?

c++ - 创建 C++ std::tuple 投影函数

c++ - 我如何在 VC++7 及更早版本中模拟 _set_abort_behavior?

C++调用基类的模板函数

c++ - c++ 模板会使程序变慢吗?