Date: Fri, 18 Oct 2024 11:13:17 +0200
czw., 17 paź 2024 o 16:58 Federico Kircheis via Std-Discussion
<std-discussion_at_[hidden]> napisał(a):
>
> Hello,
>
> I would like to have, in c++, a way to "register" objects from different
> translation units in a global container (an array).
>
> This is already possible in gcc and msvc with tools outside of the
> language, with multiple drawbacks.
>
It needs to be `array`? If we allow linked list each position can
"self append" to global list:
```
#include <functional>
#include <cstdio>
struct Node
{
inline static const Node* first = nullptr;
std::function<void()> callback;
const Node* next;
Node(std::function<void()> c) : callback{ c }
{
next = first;
first = this;
}
};
Node nameX = { []{ std::puts("test1"); } };
Node nameY = { []{ std::puts("test2"); } };
int main()
{
auto f = Node::first;
while (f) //this loop could be wrapped into "foreach" loop
{
f->callback();
f = f->next;
}
}
```
Dlls would be only problematic if you would be allowed to load and unload them.
It would probably need lot of mutex to guard updates.
<std-discussion_at_[hidden]> napisał(a):
>
> Hello,
>
> I would like to have, in c++, a way to "register" objects from different
> translation units in a global container (an array).
>
> This is already possible in gcc and msvc with tools outside of the
> language, with multiple drawbacks.
>
It needs to be `array`? If we allow linked list each position can
"self append" to global list:
```
#include <functional>
#include <cstdio>
struct Node
{
inline static const Node* first = nullptr;
std::function<void()> callback;
const Node* next;
Node(std::function<void()> c) : callback{ c }
{
next = first;
first = this;
}
};
Node nameX = { []{ std::puts("test1"); } };
Node nameY = { []{ std::puts("test2"); } };
int main()
{
auto f = Node::first;
while (f) //this loop could be wrapped into "foreach" loop
{
f->callback();
f = f->next;
}
}
```
Dlls would be only problematic if you would be allowed to load and unload them.
It would probably need lot of mutex to guard updates.
Received on 2024-10-18 09:13:29