我想知道是否有任何方法来检查您分配到std :: function的函数指针是否为nullptr.我期待着!-operator这样做,但它似乎只在函数被赋值为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;
- }
- }