我如何将函数指针作为类模板参数传递?

问题描述 投票:0回答:1

我已经声明了班级模板:

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个回答
0
投票

[创建类的实例时,使用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);
© www.soinside.com 2019 - 2024. All rights reserved.