Skip to content

Support nullable external_ptr<> #312

Open
@krlmlr

Description

@krlmlr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions