Constness behavior of Handle<Object> vs. Object

11 views
Skip to first unread message

Jakob Gruber

unread,
Apr 14, 2021, 5:51:18 AM4/14/21
to v8-...@googlegroups.com
Hi team,

I came across this yesterday and found the behavior surprising, thus this quick fyi email.

`Handle<Object>` and `Object` class members have different behavior wrt to const. The former behaves like a plain old C++ pointer (`ClassXYZ*`) while the latter behaves like an instance (`ClassXYZ`). This is a bit strange, since conceptually both `Handle<Object>` and `Object` otherwise behave like pointers underneath (e.g. `Object x; x = y;` is a reference copy, not a value-copy).

Examples:

class C {
 void foo() const { x_.nonconst_function(); }  // compile-time error.
 ClassXYZ x_;
}
   
class C' {
 void foo() const { x_->nonconst_function(); }  // okay.
 ClassXYZ* x_;
}

class C'' {
 void foo() const { x_.nonconst_function(); }  // compile-time error.
 Object x_;
}
   
class C''' {
 void foo() const { x_->nonconst_function(); }  // okay.
 Handle<Object> x_;
}

While not terribly important, equal behavior between Object and Handle<Object> would be more consistent.

Leszek Swirski

unread,
Apr 14, 2021, 7:08:45 AM4/14/21
to v8-dev
I would imagine this would be solvable by providing different overloads for const and non-const operator-> (and operator*) in Handle.

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/CAH3p7oNJesNF0CuAsGe4O1UZoZGkio1ggZw_orGW3e4FLcTEhA%40mail.gmail.com.

Jakob Gruber

unread,
Apr 14, 2021, 7:13:09 AM4/14/21
to v8-...@googlegroups.com
On Wed, Apr 14, 2021 at 1:08 PM Leszek Swirski <les...@chromium.org> wrote:
I would imagine this would be solvable by providing different overloads for const and non-const operator-> (and operator*) in Handle.

That'd make both C'' and C''' behave like C (value member), but given their similarity to pointers it may make sense to have them behave like C' (pointer member).
 
Reply all
Reply to author
Forward
0 new messages