Vorrei passare un puntatore a funzione da una matrice di puntatori a funzione come argomento modello. Il mio codice sembra compilarsi usando MSVC anche se Intellisense si lamenta che qualcosa non va. Sia gcc che clang non riescono a compilare il codice.
Considera il seguente esempio:
static void test() {}
using FunctionPointer = void(*)();
static constexpr FunctionPointer functions[] = { test };
template <FunctionPointer function>
static void wrapper_function()
{
function();
}
int main()
{
test(); // OK
functions[0](); // OK
wrapper_function<test>(); // OK
wrapper_function<functions[0]>(); // Error?
}
MSVC compila il codice ma Intellisense dà il seguente errore:invalid nontype template argument of type "const FunctionPointer"
gcc non riesce a compilare con il seguente messaggio:
<source>: In function 'int main()':
<source>:19:33: error: no matching function for call to 'wrapper_function<functions[0]>()'
19 | wrapper_function<functions[0]>(); // Error?
| ^
<source>:8:13: note: candidate: 'template<void (* function)()> void wrapper_function()'
8 | static void wrapper_function()
| ^~~~~~~~~~~~~~~~
<source>:8:13: note: template argument deduction/substitution failed:
<source>:19:30: error: '(FunctionPointer)functions[0]' is not a valid template argument for type 'void (*)()'
19 | wrapper_function<functions[0]>(); // Error?
| ~~~~~~~~~~~^
<source>:19:30: note: it must be the address of a function with external linkage
clang non riesce a compilare con il seguente messaggio:
<source>:19:2: error: no matching function for call to 'wrapper_function'
wrapper_function<functions[0]>(); // Error?
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:8:13: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'function'
static void wrapper_function()
^
1 error generated.
Domande:
È wrapper_function<functions[0]>();valido o no?
In caso contrario, c'è qualcosa che posso fare per passare functions[0]come argomento modello wrapper_function? Il mio obiettivo è quello di costruire un nuovo array di puntatori a funzione al momento della compilazione, con il contenuto { wrapper_function<functions[0]>, ..., wrapper_function<functions[std::size(functions) - 1]> }.
wrapper_function<decltype(functions[0])>()non compilare.