Support nullable `external_ptr<>`

Open
#312 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp, r
Domain
api

Research direction

Read cpp11/external_pointer.hpp and start with the external_pointer cases exercised by the cpp_source examples. Reproduce both nullptr and R_NilValue inputs in R; done means the chosen nullable behavior is consistent in both directions and existing external-pointer behavior remains valid.

Written by the indexing model from the issue text.

Description

Currently, a nullptr but not an R_NilValue can be assigned to an external_ptr<> object. This is inconsistent and surprising, in particular because the roundtrip works correctly the other way. Should we support this?

CC @hannes @TMonster.

library(cpp11)

# Works
cpp_source(
  code = '
  #include "cpp11/external_pointer.hpp"
  #include "cpp11/strings.hpp"

  [[cpp11::register]]
  cpp11::external_pointer<int> test() {
    return cpp11::external_pointer<int>(nullptr);
  }
  '
)

test()
#> NULL

# Fails, but perhaps shouldn't
cpp_source(
  code = '
  #include "cpp11/external_pointer.hpp"
  #include "cpp11/strings.hpp"

  [[cpp11::register]]
  cpp11::external_pointer<int> test() {
    return cpp11::external_pointer<int>(R_NilValue);
  }
  '
)

test()
#> Error: Invalid input type, expected 'externalptr' actual 'NULL'

Created on 2023-04-02 with reprex v2.0.2

Dominant language
C++
Stars
224
Forks
52
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from r-lib/cpp11

All issues in r-lib/cpp11

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.