Class Layoutfunction table. class B { public: virtual int f1(); virtual void f2(int); virtual int f3(int); ~~~ }; B *bp = new B; 47 B members bp vptr other stuff… &B::f1 &B::f2 &B::f3 vtbl for B B object Copyright object’s virtual function table. The call bp‐>f1(); Is translated to something like (*(bp‐>vptr)[0])(bp) 49 B members bp vptr other stuff… &B::f1 &B::f2 &B::f3 vtbl for B B object Copyright © Copyright © 2020 by Stephen C. Dewhurst and Daniel Saks 23 Virtual Calling Sequence The call bp‐>f1(); Is translated to something like (*(bp‐>vptr)[0])() Or, in other words, MOV R0,R40 码力 | 51 页 | 461.37 KB | 6 月前3
Compile-Time Validationtwo functions (f1, f2) into a new function. The return value of the previous function is passed to the next function. auto compose(auto f1, auto f2) { return [=] { return f2(f1()); }; }Composition Compose two functions (f1, f2) into a new function. Each function takes a callback to next function as argument. auto compose(auto f1, auto f2) { return [=] (auto next) { return f1([=] () { return individual functions used in its composition. auto f1 = foo | bar; auto f2 = f1 | baz; // pseudocode properties of f2 == compose( properties of f1, properties of baz )Function Composition struct0 码力 | 137 页 | 1.70 MB | 6 月前3
Beyond struct: Programming a Struct Replacement in C++20Google library These opinions are my ownStruct struct person { int id = 1; std::string name; int score = 0; }; int main() { person p{.id = 1, .name = "John"}; p.id = 2; p.name = "JRB"; std::cout << p // member<"id", int>, // member<"score", int, [](auto& self) { return get<"id">(self) + 1; }>, // member<"name", std::string, [] { return // >; Person p; std::cout << get<"id">(p) << " " << get<"name">(p) << " " << get<"score">(p) << "\n"; }Member template()> struct 0 码力 | 65 页 | 702.78 KB | 6 月前3
Quantifying Accidental Complexity: An empirical look at teaching and using C++preserves the arg’s l/rvalue-ness (incl. can move from rvalue arg) C++20 Proposed equivalent 28 void f1(int x) { g(x); } void f2(const X& x) { // for lvalues g(x); } void f2(X&& x) { // for rvalues f3(const T& t) { g(t); } // hard to overload to pass by value // hard to overload for rvalues void f1(in int x) { g(x); } void f2(in X x) { g(x); } templatevoid f3(in T t) { g(t); virtual, some path must have a non-const use of x (else use in) C++20 Proposed equivalent 30 void f1(/*inout*/ X& x) { g(x); // ok x = 42; // ok but can omit } void f2(/*inout*/ X& x) { 0 码力 | 36 页 | 2.68 MB | 6 月前3
Data Is All You Need for FusionbreakPipeline(0); subpipe = pipeline.subpipeline(0, 1); id f d i l( t Afor x { f1(); f2(); } for x { f1(); } — - — Break for y{ f2(); }Controlling Properties of the Fused Code code-samples breakPipeline(0); subpipe = pipeline.subpipeline(0, 1); id f d i l( t A for x { f1(); f2(); } for x { f1(); } — - — Break for y{ f2(); }Controlling Properties of the Fused Code code subpipe = pipeline.subpipeline(0, 1); void my fused impl(const Array a, for x { f1(); f2(); } for x { f1(); — - — Subpipeline for y{ f2(); } } code-samples manya227 June 2024 Pipeline 0 码力 | 151 页 | 9.90 MB | 6 月前3
Pipes: How Plumbing Can Make Your C++ Code More ExpressiveBRANCHING OUT: FORK fork transform(f1) transform(f2) transform(f3) results1 results2 results3 push_back push_back push_back transform(f)27 fork transform(f1) transform(f2) transform(f3) push_back push_back pipes::transform(f) >>= pipes::fork(pipes::transform(f1) >>= pipes::push_back(results1), pipes::transform(f2) >>= pipes::push_back(results2) push_back results1 set_segregate set_segregate(input1, input2, pipes::transform(f1) >>= pipes::push_back(results1), pipes::filter(p2) >>= pipes::push_back(results2),0 码力 | 61 页 | 9.52 MB | 6 月前3
COMPOSABLE C++writes writes code like this? code like this? auto compose(auto f1, auto f2) { return [f1, f2] (auto arg) { if (auto result = f1(arg); result) { return result; } return f2(arg); writes writes code like this? code like this? auto compose(auto f1, auto f2) { return [f1, f2] (auto arg) { if (auto result = f1(arg); result) { return result; } return f2(arg);0 码力 | 124 页 | 8.28 MB | 6 月前3
Delivering safe C++dereferencing • Don’t dereference an unchecked pointer • void f0(int* p) { *p = 7; } // not OK • void f1(int* p) { if (p) *p = 7; } // OK • void f2(not_nullp) { *p = 7; } // OK (not_null constructor ownership • owner is low level, prefer unique_ptr or other ownership abstractions void f1(owner p) // f1() used_to_take_a_raw_pointer { // ... delete p; // required or f() must transfer ownership owner p2) { f1(p1); // error: p1 is not an owner f2(p1); // call OK, as ever, but f2()’s definition is not f1(p2); // transfers ownership; p2 in now invalid in f3() because f1() will delete it 0 码力 | 74 页 | 2.72 MB | 6 月前3
Back to Basics: Templates Part 2std::vectorstuff; public: foobar(int n=0); ... void add(T const& t) { stuff.push_back(t); } }; void f1(); void f2(); #endif //- file_2.cpp #include "foobar.h" #include using std::string; void main() { f1(); f2(); return 0; } //- file_1.cpp #include "foobar.h" #include #include using std::string; using std::complex; using cxfloat = complex ; void f1(int n) { foobar std::vector stuff; public: foobar(int n=0); ... void add(T const& t) { stuff.push_back(t); } }; void f1(); void f2(); #endif //- file_2.cpp #include "foobar.h" #include using std::string; void 0 码力 | 80 页 | 490.15 KB | 6 月前3
Monads in Modern C++std::future std::futuref1 = ...; T2 continuationA(T1); auto f2 = f1.then(continuationA); std::future continuationB(T1); auto f2 = f1.then(continuationB); functor auto res = f1 .then(c1) std::future std::future f1 = ...; T2 continuationA(T1); auto f2 = f1.then(continuationA); std::future continuationB(T1); auto f2 = f1.then(continuationB); functor auto res = f1 .then(c1) constexpr /* maybe adaptor closure*/ transform(Callable&& c); 1: auto closure = maybe::views::transform(f1) 2: | maybe::views::and_then(f2) 3: | maybe::views::or_else(f3); 4: 5: /* some-maybe-type 0 码力 | 94 页 | 4.56 MB | 6 月前3
共 59 条
- 1
- 2
- 3
- 4
- 5
- 6













