【问题标题】:std::function has no member named 'target'std::function 没有名为 'target' 的成员
【发布时间】:2015-01-10 12:31:36
【问题描述】:

我正在尝试存储唯一函数指针的列表。一个明显的纯指针包装似乎 std::function

事实证明,std::functions cannot be compared

那么原始指针的简单比较应该可以工作,对吧?也许,但我收到以下代码的以下错误。我的编译器是 gcc 4.7.2。这是 2012 年没有实施的东西吗?

    std::function<void(bool)> f;
    void(*p)(bool) = f.target<void(*)(bool)>();

错误:“class std::function”没有名为“target”的成员


这里是除了标题之外的相关内容:

#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

【问题讨论】:

  • 你使用-std=c++11c++0x 标志吗?您还可以升级编译器和/或使用boost::function
  • 应该很容易检查编译器的 &lt;functional&gt; 标头。
  • c++11,但您的其他 cmets 持有。问题的目的是了解此消息是否意味着 .target 尚不受支持,或者我犯了一些愚蠢的错误。我目前正在尝试用我的 &lt;functional&gt; 制作一些东西,但它是 2500 LOC。
  • 嗯,你激活 RTTI 了吗?
  • @n.m.关键是能够从列表中删除这些功能。我正在处理完全相同的问题。

标签: c++11 gcc comparison std-function


【解决方案1】:

我也有这个问题。需要在编译器标志中启用 RTTI。 我在编译器标志中有-fno-rtti

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2014-06-20
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多