Budget: 1000 UAH Deadline: 1 day
Good day. I can complete the assigned task quickly and efficiently. The program will pass all tests.
Implement in C++17 a function template inject that, given two objects (possibly of different classes) S and T that implement a common interface I, will perform such manipulation of the virtual method table (vtable) of the target object so that calls to the interface methods for this object are made through the object passed as the source.
As a solution, send one header file (with the extension .h). Here is its template:
#ifndef INJECT_H_INCLUDED
#define INJECT_H_INCLUDED
#include <cstddef>
#include <type_traits>
template <class I, class S, class T,
typename std::enable_if< // enable this template only if:
std::is_polymorphic<I>::value && // - I has a vtable
std::is_base_of<I,S>::value && // - S implements I
std::is_base_of<I,T>::value, // - T implements I
int>::type = 0>
void inject(S & source, T & target)
{
// injects the implementation of interface I from source to target,
// so that calling the methods of I on target will result in them
// being called on source
typedef ptrdiff_t * vtable;
}
#endif
It can be assumed that:
the code will compile with the g++ compiler with an ABI compatible with version 4.7;
I is a class that has only virtual methods (without any fields);
I is not abstract and has a public default constructor;
classes S and T are actual (real) classes of the objects source and target;
classes S and T virtually (possibly indirectly) inherit from I;
the subobject of class I is not a strict prefix of any subobject of the objects of classes S or T;
vtable (virtual method table) of class I is not a strict prefix of vtable of any subobject of the objects of classes S or T.
For preliminary tests of your solution, you can use the following program in C++17:
#include <iostream>
#include "inject.h"
using namespace std;
struct F
{
virtual void f() { }
};
void callf(F & f)
{
f.f();
}
struct Dummy
{
virtual void dummy() { }
};
struct A : Dummy, virtual F
{
virtual void f() { cout << "A::f()" << endl; }
};
struct B : Dummy, virtual F
{
virtual void f() { cout << "B::f()" << endl; }
};
A a;
B b;
int main()
{
callf(a);
callf(b);
inject<F>(a, b);
callf(a);
callf(b);
return 0;
}The result of the above program should be:
A::f() B::f() A::f() A::f()
Budget: 1000 UAH Deadline: 1 day
Good day. I can complete the assigned task quickly and efficiently. The program will pass all tests.
Budget: 700 UAH Deadline: 1 day
Good evening!!! I can do it well!!!!!!!! Contact me!!!!!!!!!!!!
Доброго дня! в якому середовищі потрібно виконати ваше завдання? Гадаю Visual Studio 2022 тут не підійде, так як там інший ABI