我试图存储唯一函数指针列表 . 纯指针的明显包装似乎是std::function .
事实证明, std::function
s cannot be compared .
那么原始指针的简单比较应该有效,对吧?也许,但我收到以下代码的以下错误 . 我的编译器是gcc 4.7.2 . 这是2012年没有实现的吗?
std::function<void(bool)> f;
void(*p)(bool) = f.target<void(*)(bool)>();
错误:'class std :: function'没有名为'target'的成员
除了 Headers 之外,这是相关的:
#ifdef __GXX_RTTI
// [3.7.2.5] function target access
/**
* @brief Determine the type of the target of this function object
* wrapper.
*
* @returns the type identifier of the target function object, or
* @c typeid(void) if @c !(bool)*this.
*
* This function will not throw an %exception.
*/
const type_info& target_type() const noexcept;
/**
* @brief Access the stored target function object.
*
* @return Returns a pointer to the stored target function object,
* if @c typeid(Functor).equals(target_type()); otherwise, a NULL
* pointer.
*
* This function will not throw an %exception.
*/
template<typename _Functor> _Functor* target() noexcept;
/// @overload
template<typename _Functor> const _Functor* target() const noexcept;
#endif
1 回答
我也有这个问题 . 需要在编译器标志中启用RTTI . 我在编译器标志中有
-fno-rtti
.