Consider the following code with a pattern design pattern:
class A { public: void templateMethod() { doSomething(); } private: virtual void doSomething() { std::cout << "42\n"; } }; class B : public A { private: void doSomething() override { std::cout << "43\n"; } }; int main() {
I am wondering if the compiler will be able to embed / disable the member function doSomething () in cases 1 and 2. This is possible if it creates 3 different binary code fragments for templateMethod (): one without inline and 2 with A :: doSomething () or B :: doSomething () inlined (which should be called respectively in cases 3, 1, and 2)
Do you know if this optimization is required by standard, or if any compiler implements it? I know that I can achieve the same effect with a CRT template and without a virtual one, but the intention will be less clear.
source share