std::function has no member named 'target'

2019-07-04 16:56发布

I am trying to store list of unique function pointers. An obvious wrapper to pure pointers seem std::function.

As it turns out, std::functions cannot be compared.

Then a simple comparison of the raw pointers should work, right? Maybe, but I am getting the following error for the following code. My compiler is gcc 4.7.2. Is this something that was not implemented back in 2012?

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

error: 'class std::function' has no member named 'target'


Here is the relevant except from the header:

#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条回答
我想做一个坏孩纸
2楼-- · 2019-07-04 17:30

I had this problem as well. RTTI needs to be enabled in the compiler flags. I had -fno-rtti in the compiler flags.

查看更多
登录 后发表回答