Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:23:18+00:00 2026-06-18T08:23:18+00:00

Say I have the following code: template <class Derived> class Base { public: virtual

  • 0

Say I have the following code:

template <class Derived>
class Base {
public:
   virtual void foo_impl() = 0;
   void foo() {
      static_cast<Derived*>(this)->foo_impl(); //A
      (*static_cast<Derived*>(this)).foo_impl(); //B
   }
};

class Derived : public Base<Derived> {
private:
   void foo_impl() {
      bar();
   }
};

A few questions:

Will line A generate a virtual function call? Although the majority of what I can find on the internet recommends doing things this way, to me I don’t see how the compiler can do static dispatch considering that a pointer to Derived could still actually point to an object of type Derived2 where Derived2 : public Derived.

Does line B fix the issue I brought up in my previous point (if applicable)? It seems like it would, considering that now the call is not on a pointer any more and thus using *. would avoid a virtual function call. But if the compiler treats the dereferenced cast as a reference type, it could still generate a virtual function call… in that case, what is the workaround?

Does adding the C++11 final keyword to foo_impl() change how the compiler would act in either (or any other relevant) case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:23:19+00:00Added an answer on June 18, 2026 at 8:23 am

    Will line A generate a virtual function call?

    Yes. foo_impl() is virtual and Derived overrides it. Even though foo_impl() in Derived is not explicitly tagged as virtual, it is in the base class, and this is enough to make it a virtual function.

    Does line B fix the issue I brought up in my previous point (if applicable)?

    No. It does not matter if the call is on a pointer or on a reference: the compiler still won’t know whether you are invoking the function foo_impl() on an instance of a class that derives from Derived, or on a direct instance of Derived. Thus, the call is performed through a vtable.

    To see what I mean:

    #include <iostream>
    
    using namespace std;
    
    template <class Derived>
    class Base {
    public:
       virtual void foo_impl() = 0;
       void foo() {
          static_cast<Derived*>(this)->foo_impl();
          (*static_cast<Derived*>(this)).foo_impl();
       }
    };
    
    class Derived : public Base<Derived> {
    public:
       void foo_impl() {
          cout << "Derived::foo_impl()" << endl;
       }
    };
    
    class MoreDerived : public Derived {
    public:
       void foo_impl() {
          cout << "MoreDerived::foo_impl()" << endl;
       }
    };
    
    int main()
    {
        MoreDerived d;
        d.foo(); // Will output "MoreDerived::foo_impl()" twice
    }
    

    Finally:

    Does adding the C++11 final keyword to foo_impl() change how the compiler would act in either (or any other relevant) case?

    In theory, yes. The final keyword would make it impossible to override that function in subclasses of Derived. Thus, when performing a function call to foo_impl() through a pointer to Derived, the compiler could resolve the call statically. However, to the best of my knowledge, compilers are not required to do so by the C++ Standard.

    CONCLUSION:

    In any case, I believe what you actually want to do is not to declare the foo_impl() function at all in the base class. This is normally the case when you use the CRTP. Additionally, you will have to declare class Base<Derived> a friend of Derived if you want it to access Derived‘s private function foo_impl(). Otherwise, you can make foo_impl() public.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say we have following sample code in C#: class BaseClass { public virtual
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
Long story short Say I have the following code: // a class like this
let's say I have the following code in A.cpp file: template <typename T> class
Say I have the following code: class Parent { static string MyField = ParentField;
Lets say I have the following code: import collections d = collections.OrderedDict() d['foo'] =
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
Let's say we have the following code in a class: //class code TextBox t
Let's say I have the following piece of code (a simple CRTP class hierarchy).
When I try and compile the following code... #include <vector> template <class T> void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.