Idiomatic way to cast or convert between different r_vector types

Open
#264 0 comments 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
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp, r
Domain
documentation

Research direction

Start with the conversion section of the vignette at the linked “fewer implicit conversions” heading, then inspect the cpp11::as_cpp examples and the Rf_coerceVector usage described in the issue. Document the idiomatic conversion between r_vector types and clarify whether the converted vector needs protection; done means the vignette answers both questions with a supported example.

Written by the indexing model from the issue text.

Description

The following conversions results in a run time error Invalid input type, expected 'integer' actual 'logical'

// cpp11::logicals val
cpp11::integers ints(val);
cpp11::integers ints = cpp11::as_cpp<cpp11::integers>(val);

It's not clear to me why logicals cannot be re-interpreted as integers. After all it's the same internal representation.

What is an idiomatic way to convert within cpp11 when converting at R level is not an option? Is it enough to cpp11::integers X2(Rf_coerceVector(X, INTSXP));, or do I still need to protect the converted vector?

It would be great if this information could be added to the conversion section of the vignette.

Thanks!
`

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.