Assigning an attribute to an empty vector in c++ produces an error

Open
#315 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reproducing the shown C++ example and compare its behavior with R's raw(0) attribute assignment. Trace how an empty writable raw vector is represented and how attr assignment distinguishes it from NULL; done means typed empty vectors can receive attributes without the NULL error, with regression coverage for this case.

Written by the indexing model from the issue text.

Description

Thanks for your work on cpp11!

Please consider the following. In c++ the code below will produce the error: attempt to set an attribute on NULL

// Create an empty vector of raws:
cpp11::writable::raws x;
// Assign an attribute:
x.attr("my_attr") = "test";

This would make sense when x actually is a NULL, since in R the following code will produce the same error:

# Assign a NULL to 'x'
x <- NULL
# Try to set an attribute, which is not possible:
attr(x, "my_attr") <- "test"

However, I would expect the c++ code to behave as the following R code:

# Assign an empty vector of raws to 'x'
x <- raw(0)
# Set an attribute. No problems here
attr(x, "my_attr") <- "test"

which works fine.

My question is: Is this expected behaviour from cpp11? If so what would be the recommended strategy to set an attribute to an empty vector (i.e., a vector of a defined type but length zero) in c++ using cpp11. If this is not expected behaviour, can it be fixed?

My session looks like this:

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C                       LC_TIME=Dutch_Netherlands.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] cpp11_0.4.2

loaded via a namespace (and not attached):
 [1] compiler_4.1.1  magrittr_2.0.3  cli_3.4.1       tools_4.1.1     fs_1.5.0        glue_1.6.2      rstudioapi_0.13
 [8] vctrs_0.6.2     usethis_2.0.1   lifecycle_1.0.3 rlang_1.1.0     purrr_1.0.1
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.