Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:10:14+00:00 2026-06-03T07:10:14+00:00

I am working with 2 libraries. One takes in and returns std::string s while

  • 0

I am working with 2 libraries. One takes in and returns std::strings while the other uses std::vector<unsigned char>s.

It would be good if I could steal the underlying arrays from std::string and std::vector<unsigned char> and be able to move them into each other without the excessive copying.

ATM I use something like:

const unsigned char* raw_memory =
    reinterpret_cast<const unsigned char*>(string_value.c_str()),
std::vector<unsigned char>(raw_memory, raw_memory + string_value.size();

And the other way:

std::string(
    reinterpret_cast<const char*>(&vector_value[0]),
    vector_value.size());

It’d be far better to be able to define a:

std::string move_into(std::vector<unsigned char>&&);
std::vector<unsigned char> move_into(std::string&&);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:10:15+00:00Added an answer on June 3, 2026 at 7:10 am

    This is not possible.

    The vector and string class do not provide way to steal from anything else than vector or string respectively. They are not meant to exchange content.

    The problem issue is that vector and string may have widely different underlying representations. Typically in gcc for example, string use the oldish COW (Copy On Write) “optimization”, which is widely different from the typical vector representation (usually just a triple of pointers/size_t attributes).

    If you are dealing with raw bytes, blame the library that decided to put them into string, and refactor it if you can.

    Otherwise: copy. The reinterpret_cast should not be necessary because char and unsigned char have implicit casts between them (and now char is often unsigned by default).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a small project with one other developer. We are using libraries
I am working on a library that allows its users (other libraries residing in
I am working on an app which uses Commonslang.jar libraries. I was trying to
I'm working on several Jython projects using libraries written in Java. I'd like to
I'm working to a Visual C++ 2008 project which needs two libraries (A and
I'm working on a C++ project using Qt libraries, and I'm working with Eclipse
We are investigating alterntive control libraries for a new project we are working on.
I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've
I'm working on a solution with multiple projects (class libraries, interop, web application, etc)
I'm working on an iOS app where I use third party libraries. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.