C++,成员函数作为模板参数传递

C++ , Member function passed as template argument

本文关键字:参数传递 成员 函数 C++      更新时间:2023-10-16

我想传递函数 eval2(T c, T &d),它是类 Algo1 中的成员函数

Algo1.h
#ifndef ALGO1_H
#define ALGO1_H
#include "Algo2.h"
template <typename T>
class Algo1
{
    private: T a, b;
    public:
    Algo1() : a(0), b(0) {}
    Algo1(T a_, T b_) : a(a_), b(b_) {}
    void anal(T &c);
    void eval1(T c);
    void eval2(T c, T &d);
    friend void get(Algo1 &al, T &a, T &b);
};
#endif

作为 anal(T &c) 函数中的模板参数。

Algo1.hpp
#ifndef ALGO1_HPP
#define ALGO1_HPP
template <typename T>
void Algo1<T>::anal(T &c) {
    Algo2<T>::process(eval2<T>, b, c);} //Pass the  member function, wrong
template <typename T>
void Algo1<T>::eval1(T c) {     a += c; }
template <typename T>
void Algo1<T>::eval2(T c, T &d) { d = a + b + c;}
#endif

在实践中,eval2() 表示一些处理成员数据的成本函数。包含方法 process() 的 "destination" 类看起来像

Algo2.h
#ifndef ALGO2_H
#define ALGO2_H
template <typename T>
class Algo2
{
    public:
    template <typename Function>
    static void process(Function f, T &x, T &res);
};
#endif
Algo2.hpp
#ifndef ALGO2_HPP
#define ALGO2_HPP
template <typename T>
template <typename Function>
void Algo2<T>::process(Function f, T &x, T &res)  { f(x, res); } //Call passed function as static
#endif

遗憾的是,eval2(T c, T &d) 是处理成员数据的成员函数,它不能声明为静态的。但是,在类之外,如果没有对象就无法调用它。因此,函数 process() 无法将传递的函数作为静态函数调用。为了解决问题并提取数据,声明并定义了友元函数get(Algo1 &al, T &a, T &b)

template <typename T>
inline void get(Algo1 <T> &al, T &a, T &b )
{
    a = al.a;
    b = a1.b;
}

它被"内置"到非成员函数 eval3() 中

template <typename T>
inline void eval3(T c, T &d)
{
    Algo1 <T> alg;
    T a, b;
    get(alg, a, b);
}

函数 anal() 被改进为调用 eval3 而不是 eval 2 的形式为

 template <typename T>
 void Algo1<T>::anal(T &c)
 {
    Algo2<T>::process(eval3<T>, b, c); //Pass the function OK
 }

我有两个问题:

  1. 有没有更舒适的方法来传递成员函数,同时保持调用为静态?

  2. 在哪里声明和定义 get() 和 eval3() 函数以避免错误

    错误

    1 错误 LNK2019:函数"void __cdecl eval3(double,double &)"中引用的未解析外部符号"void __cdecl get(类 Algo1 &,double &,double &)"(?get@@YAXAAV?$Algo 1@N@@AAN1@Z)

非常感谢您的帮助。

_____________评论________________

第二点已经解决。而不是声明

 friend void get(Algo1 &al, T &a, T &b);

需要这样声明

 template <typename T>
 friend void get(Algo1 &al, T &a, T &b);
  1. 有没有更舒适的方法来传递成员函数,同时保持调用为静态?

使用 C++11 和 lambda,您可以这样做

template <typename T>
void Algo1<T>::anal(T &c) {
    Algo2<T>::process([=](T a1, T& a2) { return this->eval2(a1, a2); }, this->b, c);
}

之前,您必须手动创建函子,如下所示:

template<typename T>
class MyFunctor
{
public:
    explicit(Algo1<T>* algo1) : algo1(algo1) {}
    void operator () (T a1, T& a2) const
    {
        algo1->eval2(a1, a2);
    }
private:
    Algo1<T>* algo1;
};

然后:

template <typename T>
void Algo1<T>::anal(T &c) {
    Algo2<T>::process(MyFunctor<T>(this), this->b, c);
}

中作为友元函数(无模板)的类中的get函数原型和作为模板函数的定义原型不匹配。因此,未解析的外部链接器可能正在搜索友元获取函数的定义。