Bug 1881931 Comment 0 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

This C++23 change causes problems for some functions returning `nsCOMPtr` or `RefPtr` objects.

Example error when compiling Firefox with -std=c++20:

```
docshell/base/BrowsingContext.cpp:1424:14: error: conversion function from 'nsCOMPtr<nsIPrincipal>' to 'nsIPrincipal *' invokes a deleted function
 1424 |       return principal;
      |              ^~~~~~~~~
obj-aarch64-apple-darwin23.2.0/dist/include/nsCOMPtr.h:764:3: note: 'operator nsIPrincipal *' has been explicitly marked deleted here
  764 |   operator T*() const&& = delete;
      |   ^
```

Example code: https://godbolt.org/z/MaP36Mh18

```
struct A {
  int m = 1;
  operator int() const& { return m; }
  operator int() const&& = delete;
};

int f() {
  // Expected error in >= C++11 because conversion function from 'A' to 'int'
  // invokes a deleted function, `operator int() const&&`.
  return A();
}

int g() {
  A a;

  // OK in <= C++20 because `operator int() const&` is called. New error in >=
  // C++23 because conversion function from 'A' to 'int' invokes a deleted
  // function, `operator int() const&&`.
  return a;
}
```
This C++23 change causes problems for some functions returning `nsCOMPtr` or `RefPtr` objects.

Example error when compiling Firefox with -std=c++23:

```
docshell/base/BrowsingContext.cpp:1424:14: error: conversion function from 'nsCOMPtr<nsIPrincipal>' to 'nsIPrincipal *' invokes a deleted function
 1424 |       return principal;
      |              ^~~~~~~~~
obj-aarch64-apple-darwin23.2.0/dist/include/nsCOMPtr.h:764:3: note: 'operator nsIPrincipal *' has been explicitly marked deleted here
  764 |   operator T*() const&& = delete;
      |   ^
```

The deleted `nsCOMPtr` function now being invoked in C++23:
https://searchfox.org/mozilla-central/rev/f6b9653de71818550ea39fdb0cf11703c43c0cd1/xpcom/base/nsCOMPtr.h#761-764

The deleted `RefPtr` function now being invoked in C++23:
https://searchfox.org/mozilla-central/rev/f6b9653de71818550ea39fdb0cf11703c43c0cd1/mfbt/RefPtr.h#330-333

Simple code example of this C++23 build error: https://godbolt.org/z/MaP36Mh18

```
struct A {
  int m = 1;
  operator int() const& { return m; }
  operator int() const&& = delete;
};

int f() {
  // Expected error in >= C++11 because conversion function from 'A' to 'int'
  // invokes a deleted function, `operator int() const&&`.
  return A();
}

int g() {
  A a;

  // OK in <= C++20 because `operator int() const&` is called. New error in >=
  // C++23 because conversion function from 'A' to 'int' invokes a deleted
  // function, `operator int() const&&`.
  return a;
}
```

Back to Bug 1881931 Comment 0