Can we pass a char* to const string&?

c++

Solution

Yes, it's acceptable. The compiler will call the `string(const char *)` constructor and create a temporary that will be bound to `s` for the duration of the call. When the fall to `f` returns the temporary will be destroyed.

Problem

Is the following code acceptable in C++? If so, what happens? Does it create a temp string variable and pass its address? ``` void f(const string& s) { } const char kJunk[] = "junk"; f(kJunk); ```

Original source