Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
Go
Haskell
HLSL
Hook
Hylo
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
cppx source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
CppCon 2017
CppCon 2018
p1240r1
p1240r2 trunk
p2320 trunk
Options
Source code
using namespace std::experimental; //==================================================================== // Library code: implementing the metaclass functions (once) #include <memory> // Clonable types have: // - a virtual clone() that returns unique_ptr<X> where X is the type that first introduces it // consteval void clonable(meta::info source) { using namespace meta; // 1. Repeat bases and members for (auto mem : base_spec_range(source)) -> mem; for (auto mem : member_range(source)) -> mem; // 2. Now apply the clonable-specific default/requirements/generations: auto clone_type = type_of(source); // if no base has a clone() we'll use our own type bool base_has_clone = false; // remember whether we found a base clone already // For each base class... for (auto mem : base_spec_range(source)) { // Compute clone() return type: Traverse this base class's member // functions to find any clone() and remember its return type. // If more than one is found, make sure the return types agree. for (auto base_mem : member_fn_range(mem)) { if (strcmp(name_of(base_mem), "clone") == 0) { compiler.require(!base_has_clone || clone_type == return_type_of(base_mem), "incompatible clone() types found: if more than one base class introduces " "a clone() function, they must have the same return type"); clone_type = return_type_of(base_mem); base_has_clone = true; } } } // Apply generated function: provide polymorphic clone() function using computed clone_type if (base_has_clone) { // then inject a virtual overrider -> __fragment struct Z { typename(clone_type) clone() const override { return std::unique_ptr<Z>(new Z(*this)); // invoke nonpublic copy ctor } }; } else { // else inject a new virtual function -> __fragment struct Z { virtual std::unique_ptr<Z> clone() const { return std::unique_ptr<Z>(new Z(*this)); // invoke nonpublic copy ctor } }; } }; //==================================================================== // User code: using the library to write our own types (many times) #include <iostream> class(clonable) B { public: virtual void print() const { std::cout << "B"; } private: int bdata; }; class(clonable) C : public B { public: void print() const override { std::cout << "C"; } private: int cdata; }; class(clonable) D : public C { public: void print() const override { std::cout << "D"; } private: int ddata; }; int main() { std::shared_ptr<B> b1 = std::make_shared<D>(); std::shared_ptr<B> b2 = b1->clone(); b2->print(); // prints "D" } //==================================================================== // Display the generated code consteval { using namespace meta; compiler.print(reflexpr(B)); compiler.print(""); compiler.print(reflexpr(C)); compiler.print(""); compiler.print(reflexpr(D)); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree