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) // Polymorphic types have: // - only nonpublic data (and private by default) // - nonpublic copy/move if any (and protected by default) // - a public+virtual or protected+nonvirtual destructor (and public+virtual by default) // - otherwise, functions and base classes are public by default // #include <memory> consteval void polymorphic(meta::info source) { using namespace meta; // For each base class... bool base_has_virtual_dtor = false; for (auto mem : base_spec_range(source)) { // Remember whether we found a virtual destructor in a base class for (auto base_mem : member_fn_range(mem)) if (is_destructor(base_mem) && is_virtual(base_mem)) { base_has_virtual_dtor = true; break; } // Apply default: base classes are public by default if (has_default_access(mem)) make_public(mem); // And inject it -> mem; } // For each data member... for (auto mem : data_member_range(source)) { // Apply default: data is private by default if (has_default_access(mem)) make_private(mem); // Apply requirement: and the programmer must not have made it explicitly public compiler.require(!is_public(mem), "polymorphic classes' data members must be nonpublic"); // And inject it -> mem; } // Remember whether the user declared these SMFs we will otherwise generate bool has_dtor = false; bool has_default_ctor = false; bool has_copy_ctor = false; // For each member function... for (auto mem : member_fn_range(source)) { has_default_ctor |= is_default_constructor(mem); // If this is a copy or move constructor... if ((has_copy_ctor |= is_copy_constructor(mem)) || is_move_constructor(mem)) { // Apply default: copy/move construction is protected by default in polymorphic types if (has_default_access(mem)) make_protected(mem); // Apply requirement: and the programmer must not have made it explicitly public compiler.require(!is_public(mem), "polymorphic classes' copy/move constructors must be nonpublic"); } // Apply requirement: polymorphic types must not have assignment compiler.require(!is_copy_assignment_operator(mem) && !is_move_assignment_operator(mem), "polymorphic classes must not have assignment operators"); // Apply default: other functions are public by default if (has_default_access(mem)) make_public(mem); // Apply requirement: polymorphic class destructors must be // either public and virtual, or protected and nonvirtual if (is_destructor(mem)) { has_dtor = true; compiler.require((is_protected(mem) && !is_virtual(mem)) || (is_public(mem) && is_virtual(mem)), "polymorphic classes' destructors must be public and virtual, or protected and nonvirtual"); } // And inject it -> mem; } // Apply generated function: provide default for destructor if the user did not if (!has_dtor) { if (base_has_virtual_dtor) -> __fragment class Z { public: ~Z() noexcept override { } }; else -> __fragment class Z { public: virtual ~Z() noexcept { } }; } // Apply generated function: provide defaults for constructors if the user did not if (!has_default_ctor) -> __fragment class Z { public: Z() =default; }; if (!has_copy_ctor) -> __fragment class Z { protected: Z(const Z&) =default; }; } // Clonable types have: // - everything polymorphic types have // - plus 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. Compose other metaclass functions polymorphic(source); // clonable is-a polymorphic // 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 { virtual void print() const { std::cout << "B"; } int bdata; }; class(clonable) C : B { void print() const override { std::cout << "C"; } int cdata; }; class(clonable) D : C { void print() const override { std::cout << "D"; } 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