Storing function pointer in std::function

前端 未结 3 739
囚心锁ツ
囚心锁ツ 2021-02-07 15:55

I\'m trying to write a C++0x wrapper around dlopen()/dlsym() to dynamically load functions from shared objects:

class DynamicLoader
{
  public:
    DynamicLoade         


        
3条回答
  •  名媛妹妹
    2021-02-07 16:49

    Based on what I see here: http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html

    #include 
    #include 
    
    template< typename Signature >
    std::function DynamicLoader::load(std::string const& name)
    {
      namespace ft = boost::function_types;
      typedef typename ft::function_pointer< typename ft::components::type >::type fp_t;
      fp_t fun_ptr;
    
      *reinterpret_cast(&fun_ptr) = dlsym(itsHandle, name.c_str());
    
      return fun_ptr;
    }
    

    I've never used dlsym so I don't understand why the cast is being done that way and not simply casting dlsym's return like so:

    fun_ptr = reinterpret_cast(dlsym(itsHandle, name.c_str());
    

提交回复
热议问题