Our current architecture uses abstract classes to enable platform abstraction. This incurs a runtime overhead during virtual method table resolution despite the platform being fully determined at runtime.
Consider trying this approach:
Interface polymorphism
How can I define an interface class in CPP that compile-time resolves to a specific implementation to avoid the overhead of a virtual method table?
To define an interface in C++ that resolves to a specific implementation at compile time and avoids the overhead of a virtual method table (vtable), you can use templates. Here's how you can achieve this:
Template-Based Interface
Define the interface as a template class and provide a concrete implementation as a template parameter. This way, the compiler will instantiate the class with the specific implementation at compile time, avoiding the need for virtual methods.
template <typename Implementation>
class Interface {
public:
void DoSomething() {
// Delegate the call to the implementation
static_cast<Implementation*>(this)->DoSomething();
}
// Other methods can be defined similarly...
};
Concrete Implementation
Next, define a concrete class that provides the actual implementation of the interface methods:
class ConcreteImplementation {
public:
void DoSomething() {
// Actual implementation
std::cout << "Doing something in ConcreteImplementation" << std::endl;
}
// Other methods can be implemented here...
};
Using the Interface
You can then use the interface by creating an instance of the interface template with the desired implementation:
No Virtual Table Overhead: Since the methods are not virtual, there's no vtable, which eliminates the runtime overhead associated with virtual function calls.
Compile-Time Resolution: The correct method implementation is resolved at compile time, making the code faster.
Considerations:
No Polymorphism: This method doesn't support runtime polymorphism, meaning you can't swap implementations at runtime like you would with a base class and virtual methods
Code Bloat: If the template is instantiated with many different implementations, this can lead to code bloat due to multiple instantiations of the template.
Neither of these considerations affect our project as we will only use one platform at a time
This approach is ideal when you need high performance and know the implementation type at compile time.
Required Changes
We cannot provide the ConcreteImplementation in main as it violates platform abstraction. Is there a way to use concepts or templates to request an implementation from the platform layer?
Our current architecture uses abstract classes to enable platform abstraction. This incurs a runtime overhead during virtual method table resolution despite the platform being fully determined at runtime.
Consider trying this approach:
Interface polymorphism
To define an interface in C++ that resolves to a specific implementation at compile time and avoids the overhead of a virtual method table (vtable), you can use templates. Here's how you can achieve this:
Template-Based Interface
Define the interface as a template class and provide a concrete implementation as a template parameter. This way, the compiler will instantiate the class with the specific implementation at compile time, avoiding the need for virtual methods.
Concrete Implementation
Next, define a concrete class that provides the actual implementation of the interface methods:
Using the Interface
You can then use the interface by creating an instance of the interface template with the desired implementation:
Benefits:
Considerations:
This approach is ideal when you need high performance and know the implementation type at compile time.
Required Changes
We cannot provide the
ConcreteImplementation
inmain
as it violates platform abstraction. Is there a way to use concepts or templates to request an implementation from the platform layer?