5 ms·
The C++ pointer-to-member is a fairly confusing concept. What it actually is is a pair - a pointer to the instance of the struct, and a pointer to a function in
by WalterBright 3y ago
The C++ pointer-to-member is a fairly confusing concept. What it actually is is a pair - a pointer to the instance of the struct, and a pointer to a function in that struct.
D calls these delegates, and generalizes it to being a pair consisting of a "context pointer" and a "pointer to a function". The neato thing is these do not have to be struct or class member functions. They can be nested functions, where the the "context pointer" is a pointer to the stack frame of the caller. I.e., a pointer to the closure.
Hence, these become lambdas.
Lamdas and pointers-to-nested functions are completely interchangeable with pointers to members. The caller does not know the difference.
In fact, lambdas are far, far more commonly used in D than pointers-to-members.
- boguscoder 3y agoC++ pointers-to-members have no standard structure and are implementation defined. But in no implementations they have pointers to instances inside them. If that was the case Why would you need an instance (again) pointer/reference to use pointer-to-member? In most implementations they just encode an offset
- WalterBright 3y agoThe pointer to instance part is the `this` pointer. > C++ pointers-to-members have no standard structure and are implementation defined. A missed opportunity, though every C++ compiler I've examined did it the same way.
- boguscoder 3y agoBut again, ‘this’ pointer is not in any way stored inside pointer-to-member. Caller has to provide one
- deleted 3y ago[deleted]
- amluto 3y ago> The pointer to instance part is the `this` pointer. What do you mean? The this pointer isn’t computed until you try to call the pointer-to-member-function.
- AdieuToLogic 3y ago>> The pointer to instance part is the `this` pointer. > What do you mean? The this pointer isn’t computed until you try to call the pointer-to-member-function. Think of it from a compiler writer's perspective. The implicit parameter when using a pointer-to-member function is the function pointer itself. The `this` (instance) pointer must be passed explicitly when invoking it (along with whatever other parameters the function requires). Ergo: >> The pointer to instance part is the `this` pointer.
- lallysingh 3y agoPointer to what, exactly? It doesn't make sense for it to be an object instance pointer, because you can use the same pointer to member on several different objects instances. If one instance's pointer was embedded in the pointer-to-member, it wouldn't work for other instances. I have no idea what the format of a pointer-to-member is. It sure looks like a small closure.
- WalterBright 3y agoSee https://news.ycombinator.com/item?id=39812834 https://news.ycombinator.com/item?id=39812834 for a more complete explanation.
- amluto 3y agoIf I follow that link and end up here: https://www.digitalmars.com/articles/b68.html https://www.digitalmars.com/articles/b68.html I find: > C++ and D diverge here. D has the notion of a delegate, aka “fat pointer”, which is a pair consisting of a pointer to the member function and a pointer to the ‘this’ object. The virtualness of the member function was resolved at the point where the address of the member function was taken … > Alternatively, C++ has the notion of a member function pointer, which is independent of whatever object is used to call it. Such an object is provided when the member function is called Which sounds right. But you said: > The C++ pointer-to-member is a fairly confusing concept. What it actually is is a pair - a pointer to the instance of the struct, and a pointer to a function in that struct. Which is neither consistent with that text nor with how C++ works. The D concept of a pointer to member is the fat pointer that encodes this. C++’s is the horrible lambda-ish thing that can find the method starting with any compatible this pointer. Yuck.
- titzer 3y agoC# calls them delegates too, as does Virgil. Virgil uses the fat pointer trick so to avoid any heap allocations. In Virgil, class C { def m() -> int { return 33; } } var x = C.new(); // allocate a new C var y: void -> int = x.m; // delegate bound to x and m var z: C -> int = C.m; // C.m method is first-class, takes an object var t = z(x); // equivalent to x.m(); I never understood why C++ and other languages had such ugly syntax for such obvious concepts.
- titzer 3y agoAlso, FWIW, Virgil doesn't have "pointer to member" for fields; it does have a first-class getter for fields, but not a setter: class C { var f: int; } var x = C.new(); var g: C -> int = C.f; // C.f is a getter method for f var z: int = g(x);
- Kranar 3y agoI can't speak to Virgil, never used it, but C++ features try their best to adhere to the principle that you don't pay for what you don't use. It's not perfect but pointer to member functions in C++ use the simplest implementation that is possible for that given functionality. In C# delegates can not be unbound from instance variables, they must be bound at the point of creation. In C++ that is not the case which makes C++ pointer to members much more flexible. It looks like in Virgil you can have both bound and unbound pointers. You are welcome to correct me but I suspect it's implemented in such a way that you are always paying the cost of having a bound function pointer even if you only ever use it as an unbound function pointer. This would violate the principle I mentioned. In C++ if you don't mind always paying that cost, you are welcome to use std::function and it will work just as it does in the example you gave: std::function<ReturnType (Arg1, Arg2, ...)>
- titzer 3y agoIn Virgil every first-class function is a represented as a fat pointer, i.e. two words[1], for both bound and unbound delegates. It's simple to understand, the syntax is clean, and it always works the way you expect it to. It doesn't create garbage and basically means you use two registers instead of one. The unbound version even does a proper virtual dispatch for you. I understand C++'s intention, but it's doing no one any favors here and makes it really hard to build proper abstractions. Pointers to members are clunky, hard to use, and easy to screw up. AFAIU it's possible that a pointer to a member to be a single simple function pointer, but not guaranteed, and you shouldn't rely on it. It seems like a very bad tradeoff. std::function is considerably more heavyweight (and according to this https://stackoverflow.com/questions/13503511/sizeof-of-stdfunctionvoidint-type https://stackoverflow.com/questions/13503511/sizeof-of-stdfu... might be arbitrarily large); i.e. it's slow in practice and people avoid it. [1] For programs less than 4GB combined code and heap, the two pointers can be packed into a single 64-bit word (though the compiler doesn't do this currently).
- Kranar 3y agoThis isn't correct. C++ pointer to member's do not carry any instance information. They are more complex than plain pointers because they will still do proper virtual dispatching in the presence of multiple inheritance which requires more machinery than a plain function pointer, but this has absolutely nothing to do with keeping a pointer to an instance/this.
- AdieuToLogic 3y ago>> The C++ pointer-to-member is a fairly confusing concept. What it actually is is a pair - a pointer to the instance of the struct, and a pointer to a function in that struct. > This isn't correct. C++ pointer to member's do not carry any instance information. Mr. Bright's description is correct from the perspective of pointer-to-member's use, not when declared nor instantiated. Given Mr. Bright's role in creating Zortech C, Zortech C++, amongst other compilers, and having used the two specifically mentioned personally, I believe he has a proven understanding of C/C++ compiler implementations. Since one can interpret Mr. Bright's statement in the form of usage or definition, the former seems most applicable when viewed in the context of the remaining text. https://en.wikipedia.org/wiki/Digital_Mars https://en.wikipedia.org/wiki/Digital_Mars
- Kranar 3y agoNo it's just incorrect and no amount of pandering to his authority will change that. People make mistakes and this is one of them, it's not the end of the world. Avoid the temptation to believe false information just because you happen to worship someone, it reflects poorly on your ability to critically judge information.
- AdieuToLogic 3y ago> Avoid the temptation to believe false information just because you happen to worship someone, it reflects poorly on your ability to critically judge information. Ad hominem attacks are the hallmark of insecurity and your expression of same reeks of pompousness reserved for the most arrogant I have had the displeasure to engage. You know nothing about me, nor my background. I hope you take time to reflect on what you wrote above and choose to engage with others differently in the future.
- vintagedave 3y agoDelphi captures the pointer-to-instance and pointer-to-function concept too. It uses them for event handlers: delegating behaviour to other classes. To do that, method pointers are often a 'fat pointer': the pair of object instance and function. type TMethodPtr = procedure(x : Integer) of object; // 'of object' means an OO method pointer, not procedural var p : TMethodPtr = foo.bar; // Captures both foo and bar p(4); // Calls foo.bar(4) Fun: you can use them in C++ too in C++Builder's dialect via the rather ugly syntax, void(__closure * myClosure)(int); // myClosure can point to a method taking an int, returning void myClosure = pObj->func; // Assigng myClosure; this captures both pObj and the address of func myClosure(4); // Call it: this calls pObj->func(4) Syntax aside, they're neat because they match type safety by the method signature not by the type of the object on which they're called (which lets yu use them to delegate to any classes, not just ones inheriting from specific ancestors.) This bypasses contravariance constraints too. More info with a discussion on C++: https://www.codeproject.com/Articles/44874/C-Delegates-and-Borland-s-C-Builder-Event-Handling https://www.codeproject.com/Articles/44874/C-Delegates-and-B...
- p0w3n3d 3y agoI like how delegates are easy in Python. del1 = someobject.some_method_without_parentheses del2 = someotherobject.other_method del2() del1() And the other way del1 = class1.method del2 = class2.other_method del1(obj_of_class1) del2(obj_of_class2)