I am confused by the function pointer of the template function.
See
#include <cstdio>
class A
{
public:
A(){}
~A(){}
void command()
{
printf("Cmd");
}
};
template<class T>
void cmd_create()
{
T t;
t.command();
};
int main()
{
typedef void(*Funcptr)();
Funcptr f1 = &cmd_create<A>;
f1();
return 0;
}
The program output Cmd.
The assembly (-O3) shows that
.file "test.cpp"
.text
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Cmd"
.section .text.startup,"ax",@progbits
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB38:
.cfi_startproc
leaq .LC0(%rip), %rsi
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $1, %edi
xorl %eax, %eax
call __printf_chk@PLT
xorl %eax, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE38:
.size main, .-main
.ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"
.section .note.GNU-stack,"",@progbits
Obviously, &cmd_create<A> returns the A::command.
The cmd_create is defined as an empty return (void), however, the code still gets the A::command in such a weird way.
Thank you for your answer!
Obviously, &cmd_create<A> returns the A::command.?&cmd_create<A>1 gives you a pointer to the functioncmd_createwhereTgets replaced with concrete type ofA. That function callA::command. Nothing is returned from either function.Aandcmd_createare irrelevant, the only observable result is callingprintf. Godbolt has easier to digest assembly: godbolt.org/z/qKhdvoT3q