why, in following code, compiler, knowing exact type, can't inline virtual function?
using namespace std; struct base{ void fire(){ show(); } virtual void show() =0;/*{ cout << "base"; }*/ }; struct : base{ inline __attribute__((__always_inline__)) void show() { cout << "a"; } }; int main() { a; (int i=0;i<1000;i++){ a.fire(); } //a.fire(); return 0; }
can this, without crtp?
i'm not @ reading assembler code, way see example, when in main:
- 1000 loaded register
- 1 substracted,
- the address of "a" loaded ,
- the output operator called before
- jump beginning of loop.
i don't see call fire
or show
. keeps implementation show, not used. correct because has external linkage.
if put in anonymous namespace, implementation can omitted.
could put reasoning assembler code in question? reference, here generated assembler code provides link:
.lc0: .string "a" a::show(): movl $1, %edx movl $.lc0, %esi movl std::cout, %edi jmp std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) main: pushq %rbx movl $1000, %ebx .l3: movl $1, %edx movl $.lc0, %esi movl std::cout, %edi call std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) subl $1, %ebx jne .l3 xorl %eax, %eax popq %rbx ret subq $8, %rsp movl std::__ioinit, %edi call std::ios_base::init::init() movl $__dso_handle, %edx movl std::__ioinit, %esi movl std::ios_base::init::~init(), %edi addq $8, %rsp jmp __cxa_atexit
Comments
Post a Comment