【问题标题】:Use a function pointer as template function type parameter?使用函数指针作为模板函数类型参数?
【发布时间】:2020-09-27 19:25:02
【问题描述】:
#include <iostream>
#include <unordered_map>
#include <utility>
#include <typeinfo>

using namespace std;
class Handle{
    public:
    int val;
    bool getAskPrice(int& tmp) const
    {
        tmp = val;
        return true;
    }
    
    bool setAskPrice(int& tmp)
    {
        val = tmp;
        return true;
    }
};

template<class RT, class ARG>
struct convertToAFL{
    static RT to_afl(ARG);
};

template<class RT, class ARG>
struct convertFromAFL{
    static RT from_afl(ARG);
};

template<>
struct convertToAFL<float, int>
{
    static float to_afl(int& value)
    {
        return static_cast<float>(value);
    }
};

template<>
struct convertFromAFL<int, float>
{
    static int from_afl(float& val)
    {
        return static_cast<int>(val);
    }
};

struct Getter{
    template<typename TICK_D, bool (Handle::*Getter)(TICK_D&) const, typename AFL_D>
    static AFL_D getter(const Handle& handle)
    {
        TICK_D temp;
        bool exists;
        exists = (handle.*Getter)(temp);
        AFL_D x = convertToAFL<AFL_D, TICK_D>::to_afl(temp);
        return exists ? x : -1;
    }
};

struct Setter{
    template<typename TICK_D, bool (Handle::*Setter)(TICK_D&), typename AFL_D>
    static void setter(Handle& handle, AFL_D& val)
    {   
        TICK_D x;
        x = convertFromAFL<TICK_D, AFL_D>::from_afl(val);
        (handle.*Setter)(x);
    }
};

int main()
{   
    Handle h;
    float val = 20.0;
    Setter::setter<int, &Handle::setAskPrice, float>(h, val);
    std::cout<<Getter::getter<int, &Handle::getAskPrice, float>(h);
    
    //std::pair<, &Setter::setter<int, &Handle::setAskPrice, float>> x;
    return 0;
}

上面的代码按预期工作,但是,在 main() 中而不是调用函数,如何存储指向模板化 Setter:setter()Getter::getter() 的指针? 我正在尝试类似的东西

std::pair<&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>(h)> func_pair;

并且以后可以调用函数。

但我收到一个错误提示

main.cpp: In function ‘int main()’:
main.cpp:85:118: error: type/value mismatch at argument 1 in template parameter list for ‘template struct std::pair’
     std::pair<&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>(h)> func_pair;
                                                                                                                      ^
main.cpp:85:118: note:   expected a type, got ‘& setter’
main.cpp:85:118: error: template argument 2 is invalid

【问题讨论】:

    标签: c++ templates function-pointers c++03


    【解决方案1】:

    静态成员函数只是普通函数。您可以像这样存储这些指针:

    std::pair<void (*)(Handle& handle, float& val), float (*)(const Handle& handle)> 
        func_pair(&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>);
    

    【讨论】:

      【解决方案2】:

      您的问题是模板参数是一种类型,但您将值(指针)作为参数传递。相反,您可以像这样使用auto

      auto func_pair = std::make_pair(&Setter::setter<int, &Handle::setAskPrice, float>, &Getter::getter<int, &Handle::getAskPrice, float>);
      

      编辑:如果您使用C++03,std::make_pair() 仍然可用,但auto 不可用。您将需要使用一系列typedefs 手动描述类型。

      【讨论】:

        【解决方案3】:

        可以使用decltype获取指针类型。

        例子:

        std::pair<int, decltype(&Setter::setter<int, &Handle::setAskPrice, float>)> x = {
            1, &Setter::setter<int, &Handle::setAskPrice, float>
        };
        

        C++11 之前:

        std::pair<int, void(*)(Handle&, float&)> x(
            1, &Setter::setter<int, &Handle::setAskPrice, float>
        );
        

        【讨论】:

        • 我正在使用 C++ 03
        • @Sujith 好的,我添加了一个没有decltype 的版本。
        • 在 C++11 之前,您不能使用 init-list 构造 std::pair,因为它有一个 ctor。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多