我想知道是否有任何方法可以检查您分配给 an 的函数指针是否std::function
为nullptr
. 我期待!
-operator 这样做,但它似乎只在函数被分配了 type 的东西时才起作用nullptr_t
。
typedef int (* initModuleProc)(int);
initModuleProc pProc = nullptr;
std::function<int (int)> m_pInit;
m_pInit = pProc;
std::cout << !pProc << std::endl; // True
std::cout << !m_pInit << std::endl; // False, even though it's clearly assigned a nullptr
m_pInit = nullptr;
std::cout << !m_pInit << std::endl; // True
我写了这个辅助函数来解决这个问题。
template<typename T>
void AssignToFunction(std::function<T> &func, T* value)
{
if (value == nullptr)
{
func = nullptr;
}
else
{
func = value;
}
}