Home > Back-end >  c Empty template function returns non-empty function pointer
c Empty template function returns non-empty function pointer

Time:04-09

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,            
  • Related