Advertisement
kenpusney

trait.cpp

Jul 26th, 2022 (edited)
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <cstdio>
  2. #include <typeinfo>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. template<typename T>
  7. concept Concept = requires(T a) {
  8.   a.doIt();
  9. };
  10.  
  11. struct Trait {
  12.   void doIt() {
  13.     cout << "do it" << endl;
  14.   }
  15. };
  16.  
  17. struct VirtualBase {
  18.   virtual void doIt() {
  19.     cout << "do it virtual" << endl;
  20.   }
  21. };
  22.  
  23. struct Impl : Trait, VirtualBase {
  24.   void doIt() {
  25.     cout << "do it in impl" << endl;
  26.   }
  27. };
  28.  
  29.  
  30. int main() {
  31.   Impl impl {};
  32.   Trait& trait = impl;
  33.   VirtualBase& virtualBase = impl;
  34.   Concept auto& concept_ = impl;
  35.  
  36.   cout << typeid(concept_).name() << endl;
  37.  
  38.   impl.doIt();
  39.   trait.doIt();
  40.   virtualBase.doIt();
  41.   concept_.doIt();
  42.  
  43.   cout << "sizeof(Impl&): " << sizeof(Impl&) << endl
  44.        << "sizeof(Trait&): " << sizeof(Trait&) << endl
  45.        << "sizeof(VirtualBase&): " << sizeof(VirtualBase&) << endl
  46.        << "sizeof(Concept auto&): " << sizeof(decltype(concept_)) << endl;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement