Assigning an attribute to an empty vector in c++ produces an error
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
- Domain
- backend-api-design
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from r-lib/cpp11
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
NVIDIA/cuda-samples#453 ·