4

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
Community
  • 1
  • 1
Vorac
  • 8,726
  • 11
  • 58
  • 101
  • Do you use `-std=c++11` of `c++0x` flag? You could also upgrade the compiler and/or use `boost::function`. – Ivan Aksamentov - Drop Nov 13 '14 at 11:29
  • Should be easy enough to check the `` header of your compiler. – Sebastian Redl Nov 13 '14 at 11:38
  • `c++11`, but your other comments hold. The purpose of the question is to know if this message means that `.target` is yet not supported, or I am doing some silly mistake. I am currently trying to make something out of my ``, but it's 2500 LOC. – Vorac Nov 13 '14 at 11:39
  • Well, did you activate RTTI? – filmor Nov 13 '14 at 12:06
  • @n.m. The point is the ability to remove those functions from the list. I'm dealing with the exact same problem. – Paulius Liekis Aug 12 '17 at 08:22
  • Because I want to provide a generic API for adding callbacks. Like in C# - you can provide an event delegate and add/remove callbacks with += and -= no matter whether they are static or non-static functions. std::function is supposed to help you with that, although it doesn't help with removing from the list. – Paulius Liekis Aug 17 '17 at 06:17
  • I have the same issue: I really need to know what function the std::function is holding for verification, and I'm unable to because it "requires" RTTI - although all I want is the function address. – gonzo Jul 01 '20 at 21:44

1 Answers1

3

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

Adam Casey
  • 949
  • 2
  • 8
  • 24