【问题标题】:How do I pass a function pointer as a class template parameter?如何将函数指针作为类模板参数传递?
【发布时间】:2020-03-11 21:22:08
【问题描述】:

我已经声明了一个类模板:

template <typename DeallocFunction, typename CryptoObject>
class CryptoDeallocator
{
    uint32_t (*DeallocFunc)(CryptoObject*);
    CryptoObject *ObjectToDealloc;
public:
    CryptoDeallocator(DeallocFunction i_p_func, CryptoObject *i_st_CryptoObject)
    {
        DeallocFunc = i_p_func;
        ObjectToDealloc = i_st_CryptoObject;
    }
    ~CryptoDeallocator()
    {
        if ((ObjectToDealloc != NULL) && (DeallocFunc != NULL))
        {
            DeallocFunc(ObjectToDealloc);
        }
    }
};

在我的代码的其他地方,我定义了一个具有以下原型的函数:

uint32_t nrf_crypto_ecc_private_key_free(nrf_crypto_ecc_private_key_t * p_private_key);

我尝试使用以下方法创建我的 CryptoDeallocator 类的实例:

nrf_crypto_ecc_private_key_t st_OwnPrivateKey;
CryptoDeallocator<uint32_t(*nrf_crypto_ecc_private_key_free)(nrf_crypto_ecc_private_key_t*), nrf_crypto_ecc_private_key_t> st_CryptoDeallocator(nrf_crypto_ecc_private_key_free, &st_OwnPrivateKey);

但我在 IAR 中收到编译错误:错误 [Pe018]:预期为“)”。

我应该使用什么正确的语法来实例化 CryptoDeallocator 类对象?

【问题讨论】:

    标签: c++ function-pointers class-template


    【解决方案1】:

    创建类的实例时,使用 decltype 说明符:

    CryptoDeallocator<decltype(nrf_crypto_ecc_private_key_free), nrf_crypto_ecc_private_key_t> st_CryptoDeallocator(nrf_crypto_ecc_private_key_free, &st_OwnPrivateKey);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      相关资源
      最近更新 更多