Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C-Style strings to std::string conversion clarification

Tags:

c++

string

copy

I've got a couple questions that I think will be quite easy for someone with C++ experience to answer, I'll bold the quesitons for the TL;DR

Given the following code:

void stringTest(const std::string &s)
{
    std::cout << s << std::endl;
}

int main()
{
    stringTest("HelloWorld");
}

Hopefuly someone can point out the error in my thought process here:

Why does the parameter in stringTest have to be marked const when passed a C-Style string? Isn't there an implicit conversion to an std::string that takes place using its cstyle string constructor, therefore "s" is no longer a reference to a literal (and is not required to be const).

Furthermore, what would a cstyle string constructor look like, and how does the compiler know to invoke this upon seeing:

stringTest("HelloWorld");

Does it simply recognize a string literal to be something like a char*?

I've stumbled upon these questions while studying copy constructors. Another quick quesiton for my own clarification...

In the case of something like:

std::string s = "HelloWorld";

Is the cstyle string constructor used to instantiate a temporary std::string, and then the temporary string is copied into "s" using the string copy constructor?:

std::string(const std::string&);
like image 284
Riken Avatar asked Apr 11 '12 15:04

Riken


People also ask

What is a difference between the std::string and C-style strings?

std::string is compatible with STL algorithms and other containers. C strings are not char * or const char * ; they are just null-terminated character arrays. Even string literals are just character arrays.

Is there std::string in C?

The std::string class manages the underlying storage for you, storing your strings in a contiguous manner. You can get access to this underlying buffer using the c_str() member function, which will return a pointer to null-terminated char array. This allows std::string to interoperate with C-string APIs.

How do I convert Wstring to CString?

The easiest solution is to use Unicode string literals and std::wstring: wstring z = L"nüşabə"; CString cs(z. c_str()); nameData. SetWindowTextW(cs);

What is the C-style character string?

A C-style string is simply an array of characters that uses a null terminator. A null terminator is a special character ('\0', ascii code 0) used to indicate the end of the string. More generically, A C-style string is called a null-terminated string.


1 Answers

Why does the parameter in stringTest have to be marked const when passed a C-Style string?

It only has to when the parameter is a reference, since a temporary std::string is constructed from the char const* you pass in and a non-const reference to a temporary is illegal.

Does it simply recognize a string literal to be something like a char*?

A string literal is a char const array, which decays to char const*. From that, the compiler infers that it should use the non-explicit constructor std::string::string(char const *) to construct the temporary.

Is the cstyle constructor used to instantiate a temporary std::string, and then the temporary string is copied into "s" using the string copy constructor?

It's a bit more complicated than that. Yes, a temporary is created. But the copy constructor may or may not be called; the compiler is allowed to skip the copy construction as an optimization. The copy constructor must still be provided, though, so the following won't compile:

class String {
    String(char const *) {}
  private:
    String(String const &);
};

int main()
{
    String s = "";
}

Also, in C++11 the move constructor will be used, if provided; in that case, the copy constructor is not required.

like image 77
Fred Foo Avatar answered Sep 21 '22 05:09

Fred Foo