1

I downgraded gcc compiler for 4.9.2 to 4.4.1, as I know 4.4.1 doesn't support lambda expressions. In the following code, a lambda expression [](void*d){ dlclose(d); } is used and facing error due to lambda expression. Can anyone help how to use below code without lambda expression?

using libs_t = std::unique_ptr<void,std::function<void(void*)>>;

I replaced the above line with the below but it's also not supported by gcc 4.4.1

typedef std::unique_ptr<void,std::function<void(void*)>> libs_t ;

m_libs[ lib_name ] = libs_t ( handle, [](void*d){ dlclose(d); } );
5
  • Does your compiler support std function? Commented Nov 2, 2017 at 12:23
  • 1
    @user3906620 Just define a function with such a body as the lambda expression Commented Nov 2, 2017 at 12:23
  • yes it supports std Commented Nov 2, 2017 at 12:23
  • @VladfromMoscow, can you explain how ? Commented Nov 2, 2017 at 12:24
  • Can you provide the declaration of dlclose? Why is the lambda even needed? Commented Nov 2, 2017 at 12:39

2 Answers 2

3

You can redefine unique_ptr specialization to accept raw function pointer instead of std::function:

typedef std::unique_ptr<void, int ( * )(void *)> libs_t ;
libs_t(handle, &dlclose);

Use of std::function in this case seems to be redundant because the only item that is supposed to be stored in it is a pointer to dlclose function.

Sign up to request clarification or add additional context in comments.

2 Comments

After above changes, error is static assertion failed: "constructed with null function pointer deleter"
@user3906620 This means that you are creating libs_t instances using default constructor. This is prohibited when deleter is a pointer to function. To fix this create libs_t instances supplying nullptr and a pointer to dlclose as parameters.
1

Just try dlclose.

No, really. It may run into problems with void return values, but if that happens write

void mydlclose(void* p){ dlclose(p); }

and use mydlclose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.