Skip to main content
Tweeted twitter.com/StackSoftEng/status/861101936757665792
Fixed spelling
Source Link
Giorgio
  • 19.7k
  • 16
  • 87
  • 137

Dynamic disbatchdispatch with captured data in C?

I'm trying to reconcile my object-oriented/functional mind with programming in a language with C. Let's say I want to achieve dynamic disbatchdispatch in C, say I want to have a collection of tasks to execute. I could make a pointer to a collection of functions I want to execute, simple enough. However, let's say that the task to be executed depends on variables that exist when the function is created, and will not be passed into the function by whatever executes them. In Java or C++, I would use an object (probably a lambda) that captures those variables, so that the function can access them when it's being executed. But I hear that function pointers cannot capture variables in standard C. So how would I implement this design pattern? Thanks.

Dynamic disbatch with captured data in C?

I'm trying to reconcile my object-oriented/functional mind with programming in a language with C. Let's say I want to achieve dynamic disbatch in C, say I want to have a collection of tasks to execute. I could make a pointer to a collection of functions I want to execute, simple enough. However, let's say that the task to be executed depends on variables that exist when the function is created, and will not be passed into the function by whatever executes them. In Java or C++, I would use an object (probably a lambda) that captures those variables, so that the function can access them when it's being executed. But I hear that function pointers cannot capture variables in standard C. So how would I implement this design pattern? Thanks.

Dynamic dispatch with captured data in C?

I'm trying to reconcile my object-oriented/functional mind with programming in a language with C. Let's say I want to achieve dynamic dispatch in C, say I want to have a collection of tasks to execute. I could make a pointer to a collection of functions I want to execute, simple enough. However, let's say that the task to be executed depends on variables that exist when the function is created, and will not be passed into the function by whatever executes them. In Java or C++, I would use an object (probably a lambda) that captures those variables, so that the function can access them when it's being executed. But I hear that function pointers cannot capture variables in standard C. So how would I implement this design pattern? Thanks.

Source Link
Phoenix
  • 758
  • 7
  • 14

Dynamic disbatch with captured data in C?

I'm trying to reconcile my object-oriented/functional mind with programming in a language with C. Let's say I want to achieve dynamic disbatch in C, say I want to have a collection of tasks to execute. I could make a pointer to a collection of functions I want to execute, simple enough. However, let's say that the task to be executed depends on variables that exist when the function is created, and will not be passed into the function by whatever executes them. In Java or C++, I would use an object (probably a lambda) that captures those variables, so that the function can access them when it's being executed. But I hear that function pointers cannot capture variables in standard C. So how would I implement this design pattern? Thanks.