boost::openmethod::final_virtual_ptr
Create a virtual_ptr for an object of a known exact class.
Synopsis
Declared in <boost/openmethod/core.hpp>
template<
class Registry,
typename Arg>
auto
final_virtual_ptr(Arg&& obj);
Description
Creates a virtual_ptr to an object, setting its v‐table pointer according to the declared type of its argument. Assumes that the static and dynamic types are the same. Sets the v‐table pointer to the registry::static_vptr for the class.
Class is not required to be polymorphic.
Nothing is looked up at runtime. Constructing a virtual_ptr from a reference or a pointer reads the object's dynamic type through the registry's rtti policy, then finds the v‐table through its vptr policy; here the v‐table pointer is a static variable, read directly. It is also the only way to create a virtual_ptr in a registry that uses policies::static_rtti, which has no dynamic type to consult and disables the constructors that would need one.
If runtime checks are enabled, and the argument is polymorphic, checks if the static and dynamic types are the same. If not, calls the error handler with a final_error value, then terminates the program with abort.
Errors
-
final_errorThe static and dynamic types of the object are different.
Example
// classes not required to be polymorphic
struct Animal {};
struct Cat : Animal {};
struct Dog : Animal {};
BOOST_OPENMETHOD_CLASSES(Animal, Cat, Dog);
Dog snoopy;
virtual_ptr<Animal> animal = final_virtual_ptr(snoopy);
std::cout << poke(animal) << "\n"; // bark
Cat felix;
animal = final_virtual_ptr(felix);
std::cout << poke(animal) << "\n"; // hiss
Template Parameters
Name |
Description |
Registry |
A |
Arg |
The type of the argument. |
Parameters
Name |
Description |
obj |
A reference to an object. |
Created with MrDocs