Trevor
/notes
Morgan
Wow
Nomid Íkorni-Sciurus
Why template functions can't be virtual?!
I guess it's because the compiler wouldn't know where to address the function implementation
Nomid Íkorni-Sciurus
When a method is declared virtual it's expected to be implemented in another point of the application. Though, template parameters can't be linked to other template definitions (T in something.cxx wouldn't be the same T in else.cxx)
MᏫᎻᎯᎷᎷᎬᎠ
Dynamically dispatched?!
olli
From C++ Templates - the complete Guide Member function templates cannot be declared virtual. This constraint is imposed because the usual implementation of the virtual function call mechanism uses a fixed-size table with one entry per virtual function. However, the number of instantiations of a member function template is not fixed until the entire program has been translated. Hence, supporting virtual member function templates would require support for a whole new kind of mechanism in C++ compilers and linkers. In contrast, the ordinary members of class templates can be virtual because their number is fixed when a class is instantiated
olli
Dynamically dispatched?!
The exact function to call is selected on runtime and not compile time. That's why polymorphism works .
Nomid Íkorni-Sciurus
This not true, virtual tells the function needs to be dynamically dispatched
It behaves that way though while you can make it pure, you could also give it a default behaviour The function has to be implemented by the non-abstract child
olli
Hm, so I was right
I wouldn't say so unless I got your explanation wrong
Nomid Íkorni-Sciurus
I wouldn't say so unless I got your explanation wrong
I guessed that since template parameters represent non-concrete types (you don't actually know what T is) you can't compare the prototypes in all of the potentially infinite virtual implementations of the method. The templates represent different things and can't be linked
olli
What does he mean by number of instantiations of a member function?
For every template parameters a instantiation is made. For example let's say you call your method with int, float and char so you will have 3 instantiations
MᏫᎻᎯᎷᎷᎬᎠ
So the fixed-size table depends on the instantiation of the its virtual function's parameters!!
Nomid Íkorni-Sciurus
When linking you know exactly what T is replaced with
I'm confused then. The compiler can't predict where to address a generic type
klimi
.
MᏫᎻᎯᎷᎷᎬᎠ
.
.
olli
Let's say we have a template function template <class T> void foo(T&&) {}; and we call it with foo(1); foo('1'); foo(1.0); the compiler generates three instantiations of the function foo, e.g. void foo<int>(int&&): ret void foo<char>(char&&): ret void foo<double>(double&&): ret ——————————————————————————- Now for "vtables". Let's say we have a class A and B class A { public: virtual void foo() {} }; class B : public A { public: void foo() override {} }; The compiler generates "vtables" which store the function to be called vtable for B: .quad 0 .quad typeinfo for B .quad B::foo() vtable for A: .quad 0 .quad typeinfo for A .quad A::foo() The code B b; A& rb = b; rb.foo(); results in a call to the function stored at OFFSET FLAT:vtable for B+16 which is in this case B::foo. Since the function to call is lookep up this is called "dynamic dispatch" or "late dispatch" * Note: The C++ standard does not define the term "vtable" or require them, that's the current implementation used by all common vendors.
olli
And there is the limitation, for A::foo to be a templated function we would need to be able to store A::foo<int>, A::foo<char> and so on. Since there can be many more instantiations the number of entries in the vtable is not fixed until the whole programm is translated which requires the vtable generation to be deferred [1]. [1] C++ Templates - the complete Guide
Nomid Íkorni-Sciurus
Ah. I was missing the practical part.
olli
What does .quad 0 means
quad determines the size of the "storage block", 0 is the value for it.. this is completely implementation specific
olli
In this case its quadword so 64bits, make sense using a 64bit system.
olli
You do mean A& rb = b Right?!
nice find - yes I did - sry (fixed)
우빈(KR)
??
olli
Why B+16?!
the VTable contains 3x 8 Byte values, the first 0, then a pointer to the typeinfo for the class and a pointer to the function to be called. 16 is the offset for the third entry into the table (the location of the function to be called)
MᏫᎻᎯᎷᎷᎬᎠ
/report
ARUN
Sry
ARUN
Wrong place
olli
Yes indeed
olli
/report
thanks
MᏫᎻᎯᎷᎷᎬᎠ
I'm just gonna note what you've told me
MᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
Thank you olli
Xavi
Can we perform arithmetic operation on struct object in c
Xavi
?
olli
If you have a 2 virtual functions The table will be 4x 8 bytes?!
Most likely yes. Although all the "vtables" are completely implementation dependant
Anonymous
.long
Got it. Thank you😊
Nomid Íkorni-Sciurus
@ollirz I'm sorry, I was unaware of the compiler's actual output.
MᏫᎻᎯᎷᎷᎬᎠ
Hold a minute
MᏫᎻᎯᎷᎷᎬᎠ
3x 8 byte What does the first has?!
MᏫᎻᎯᎷᎷᎬᎠ
8-15 is the offset for the typeinfo pointer
MᏫᎻᎯᎷᎷᎬᎠ
Then What 0-7 refers to?!
olli
Then What 0-7 refers to?!
The Itanium ABI specifies the first byte is an offset value that is useful when dynamic casting between objects.
Liam
Can we perform arithmetic operation on struct object in c
No. C doesn't support operator overloading.
MᏫᎻᎯᎷᎷᎬᎠ
Okay What is itanium ABI XD?!
Sorry for a lot of questions
Liam
Sorry for a lot of questions
http://refspecs.linuxbase.org/cxxabi-1.83.html
olli
Okay What is itanium ABI XD?!
Basically an approach to have a "common standard" and some compatibility on binary level.
MᏫᎻᎯᎷᎷᎬᎠ
Okay
MᏫᎻᎯᎷᎷᎬᎠ
Class?
olli
To store the 0 or the pointer to the functions and typeinfo. .quad 0 basically means "here are 64 bits all set to 0"
Anonymous
Then What 0-7 refers to?!
Can i know which one you refer too?? I am new to group and 8 -15??
MᏫᎻᎯᎷᎷᎬᎠ
Can i know which one you refer too?? I am new to group and 8 -15??
I dunno here But he said that itanium ABI specifies it when dynamic casting
Anonymous
MᏫᎻᎯᎷᎷᎬᎠ
What?!
MᏫᎻᎯᎷᎷᎬᎠ
No
MᏫᎻᎯᎷᎷᎬᎠ
We didn't mean that
MᏫᎻᎯᎷᎷᎬᎠ
There are 5 bytes left
MᏫᎻᎯᎷᎷᎬᎠ
Hold on
MᏫᎻᎯᎷᎷᎬᎠ
I think i get it