GithubHelp home page GithubHelp logo

should cpp11 support string_view? about cpp11 HOT 4 CLOSED

r-lib avatar r-lib commented on August 20, 2024
should cpp11 support string_view?

from cpp11.

Comments (4)

jimhester avatar jimhester commented on August 20, 2024

I don't think it is entirely trivial to do this. First as you mentioned we would need to use compatibility library so we could use it with C++11.

Second and perhaps more importantly cpp11 uses translateCharUT8() to access character data from R vectors. This translates the characters to UTF-8 and (if needed) allocates memory that reclaimed at the end of the enclosing .Call(). If we use string_view() on this data and the string_view() outlasts the .Call() boundary the data will no longer be valid.

It might be that this particular case is rare enough and the benefits of string_view are substantial enough that it makes sense, I am not sure.

from cpp11.

jimhester avatar jimhester commented on August 20, 2024

Another pretty serious issue with using string_view in cpp11 is that nearly all R APIs that have const char* expect null terminated strings, which may not be the case with a string_view.

from cpp11.

CreRecombinase avatar CreRecombinase commented on August 20, 2024

Yeah the point about possible allocation by translateCharUTF8 totally makes sense. I guess I was thinking of string_view as modeling the contents of a CHARSXP "as it is". Operationally, I think of a string_view as

  1. non-owning
  2. cheap to copy
  3. immutable (unlike other "data" SEXP, you can't muck easily around with the contents of a CHARSXP in-place because of the CHARSXP cache. string_view, unlike its cousin span, will only give you a const pointer to its contents)
  4. "knowing" its own length

from cpp11.

jimhester avatar jimhester commented on August 20, 2024

Closing this issue for now, while I do think using string_view is a nice idea in theory, I don't think it really will work in practice for cpp11 due to the concerns expressed above.

from cpp11.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.