Honestly, what I read said that the VFT is created during compile time and that is EXACTLY how it knows which function to call during run time and that it leaves NO ambiguity. |
That is correct. It uses the VFT to look up which function to call
at runtime.
But to be able to inline the function call it needs to know which function to call
at compile time.
Because if Base was sent as pass by value, pointer, or reference, it will call base methods. If derived was passed as base ref or base pointer, then it calls derived overriding method. |
Correct
So what you are telling me is that the compiler doesn't like to look too deeply at the VFT when it is trying to inline? |
If the compiler knows the type of the objects and it's always the same type then there is no problem with inlining.
The way inlining works is essentially that you replace the function call with the content of the function itself. How do you do that if the function call expression can invoke different functions depending on what's happening at runtime?
Disregard what the compiler does for one second with inlining, if you as a programmer just looked at the items to be inlined and the VFT during compile time, do you have all the info to figure out if it can be inlined yourself and its just that the compiler has the info but might not want to do it?
|
I think it should be at least theoretically possible to analyse the code to figure out whether or not inlining is possible but in practice it depends heavily on the compiler and the compiler settings.
Inlining can easily increase the code size which is a big reason why it's not always done even if possible. GCC will inline much more aggressively when using the -O3 compiler flag which often leads to larger executable file compared to -O2.
If the function is not defined in the same translation unit then inlining is generally not possible unless link time optimization (LTO) is enabled.
A) "set" is a class that is templatized. |
The official term is "class template".
In other words,
std::set not a class. It's a template for generating classes.
You can't simply write "set s2{ 2,5,1,3,2,1,4 };" because it needs the type of template. So what can be said about "set<int>" in here? |
As seeplus mentioned, what happens here is that the template arguments are deduced.
The type of s2 is still
std::set<int>. This is the same type as
std::set<int, std::less<int>, std::allocator<int>>.
Is it fair to call it a type here or do we call it a class |
std::set<int> is a class. It's also a type because classes are types.
B) MyClass classObj;
"MyClass" here is a class, but it can also be considered a type, because classes are custom types. |
Correct.
(SortDescend<int>) This sends in a type, which is REALLY a struct that is templatized for int type. |
Template arguments are passed inside
< and
>. This is where you might find
SortDescend<int>.
Function/constructor arguments are passed inside
( and
). You won't find
SortDescend<int> there. You might find
SortDescend<int>() but then it's no longer a type but an object.
D)vector<int>::iterator iter;
When I first saw this I thought the left of :: was the class |
It is.
and that it called method iterator that then somehow returned an object. |
std::vector<int>::iterator is a type.
You can define classes and other types inside a class. You can also define static member variables and static member functions. You don't need an object of the class to use those.