Why can't templates be within extern “C” blocks?

前端 未结 5 1762
轮回少年
轮回少年 2020-11-30 11:06

This is a follow-up question to an answer to Is it possible to typedef a pointer-to-extern-“C”-function type within a template?

This code fails to compile w

5条回答
  •  心在旅途
    2020-11-30 11:30

    Because extern C disables name mangling, which templates use

    To see that templates are implemented with name mangling, compile and decompile:

    #include 
    
    template 
    C f(C i) { return i; }
    
    int main() {
        f(1);
        f(1.5);
    }
    

    with:

    g++ -c -g -std=c++98 main.cpp
    objdump -Sr main.o
    

    The output contains:

    int main() {
       0:   55                      push   %rbp
       1:   48 89 e5                mov    %rsp,%rbp
       4:   48 83 ec 10             sub    $0x10,%rsp
        f(1);
       8:   bf 01 00 00 00          mov    $0x1,%edi
       d:   e8 00 00 00 00          callq  12 
                e: R_X86_64_PC32    _Z1fIiET_S0_-0x4
        f(1.5);
      12:   48 b8 00 00 00 00 00    movabs $0x3ff8000000000000,%rax
      19:   00 f8 3f 
      1c:   48 89 45 f8             mov    %rax,-0x8(%rbp)
      20:   f2 0f 10 45 f8          movsd  -0x8(%rbp),%xmm0
      25:   e8 00 00 00 00          callq  2a 
                26: R_X86_64_PC32   _Z1fIdET_S0_-0x4
    }
      2a:   b8 00 00 00 00          mov    $0x0,%eax
      2f:   c9                      leaveq 
      30:   c3                      retq
    

    Note how all callq were made to call weird names like _Z1fIiET_S0_.

    The same goes for other features which depend on name mangling, e.g. function overloading.

    I have written a more detailed answer at: What is the effect of extern "C" in C++?

提交回复
热议问题