Обходной путь для «использования переменной перед вычетом auto»C++

Программы на C++. Форум разработчиков
Ответить
Гость
 Обходной путь для «использования переменной перед вычетом auto»

Сообщение Гость »


I am creating wrapper functions, that modify existing functions (targets). The targets are determined at runtime as pointers without type information (). Each of them has different parameters and return type. The wrapped functions are called from external code, so they have to match the signature of the target.

The basic scheme is like this:

Код: Выделить всё

bool wrap(void* f, void** t) {     *t = magic();  // target determined at runtime as void*     return true; } // define new wrapping function and storage for target int(*foo_target)(int); int foo(int data) {     return foo_target(data) + 10; } auto res = wrap(reinterpret_cast(&foo), reinterpret_cast(&foo_target)); 
The function is a placeholder, that does all the heavy lifting. For each target I define a wrapper function () and a variable to store the function pointer to the target function with the correct type (

Код: Выделить всё

foo_target
).

This works, but requires ugly type casts and I have to keep the signatures of

Код: Выделить всё

foo_target
and in sync manually. What I would like to have is something like this:

Код: Выделить всё

template  struct wrap {     F* target;     wrap(F&& f)     {         target = reinterpret_cast(magic());     } }; static auto foo = wrap(     [](int data) -> int   //  int        // 

Источник: [url]https://stackoverflow.com/questions/78128208/workaround-for-use-of-variable-before-deduction-of-auto[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»