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 7165019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:10:17+00:00 2026-05-28T14:10:17+00:00

i just wrote a function: void doSomeStuffWithTheString(const std::string& value) { … std::string v =

  • 0

i just wrote a function:

void doSomeStuffWithTheString(const std::string& value) {
...
std::string v = value;
std::cout << value.c_str();
...
}

but then i call this with

doSomeStuffWithTheString("foo");

and it works. So i would have thought that this to work (a const char* to initialise a implicit instance of std::string) the value would have to be passed by value, but in this case is passed by (const) reference.

Is by any chance a implicit temporal std::string instantiated from const char* when the reference is const? if not, then how this possibly work?

EDIT

what happens if the function is overloaded with

void doSomeStuffWithTheString(const char* value);

which one will choose the compiler?

  • 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-05-28T14:10:17+00:00Added an answer on May 28, 2026 at 2:10 pm

    The std::string type has an implicit conversion (via constructor) from const char*. This is what allows the string literal "foo" to convert to std::string. This results in a temporary value. In C++ it’s legal to have a const & to a temporary value and hence this all holds together.

    It’s possible to replicate this trick using your own custom types in C++.

    class Example {
    public:
      Example(const char* pValue) {}
    };
    
    void Method(const Example& e) {
      ...
    }
    
    Method("foo");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a function along the lines of this: void myFunc(myStruct *&out) { out
I wrote a function that looks like this: - (void)changeText:(NSUInteger)arrayIndex; Let's just say that's
I just wrote a stored function to calculate the working days between two dates.
I just wrote the following C++ function to programmatically determine how much RAM a
I've just had to write a string reverse function in C# 2.0 (i.e. LINQ
I just wrote an if statement in the lines of if (value == value1
I just wrote some C code: #include <stdlib.h> #include <time.h> #include <string.h> typedef struct
I wrote a template function for reading in string or numerical data from files
Here is what I just wrote: public void mutate(){ ListIterator<Double> git = genome.listIterator(); Iterator<Double>
Hey i'm just trying to write some code in DrScheme: ((function (x) (* x

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.