string s = "abc";
The above statement will first invoke string ( const char * s ) constructor, then invoke copy constructor according to What are the differences in string initialization in C++? .
Here is the question: how C++ know it should invoke string ( const char * s ) to convert literal string "abc" to a temporary string object?
Note:
copy constructor won't be invoked in copy initialization.
Initializing an object by using the syntax
string s = "abc";
is called copy initialization.
There are several scenarios where that is legal initialization. In all cases the RHS must be convertible to a string for it to work.
One way a string literal can be converted to a string is through the constructor of string that takes a char const*. That is called a user defined conversion.
Summary of all answers:
Reference:
http://en.cppreference.com/w/cpp/language/implicit_cast
http://en.cppreference.com/w/cpp/language/copy_initialization
http://en.cppreference.com/w/cpp/language/converting_constructor
Related
Are strings defined as
string s="Hello";
is of type const char[] of size 6?
If it is of type const char[] then how am I able to do such thing like
s[0]='i'; ??
In
std::string s = "Hello";
the type of the right-hand side is const char[6]. The five characters (not including the terminating null character) are copied elsewhere by the std::string object's constructor. That is why you are able to modify s later.
The type of a string literal is const char [N].
But when you write string s = "Hello";, s is not a literal. It's simply an object of type std::string.
The only literal here is "Hello", thus you can't do "Hello"[1] = 'a';.
In C++, if string is a class, why do we not need the dot operator or an object to store data in a string?
Classic string:
string str = "ABC";
Why can we directly pass ABC using " " instead of doing it like
string str;
str.data = "ABC";
But we need to use objects to access the functions.
Example:
str.length();
Why do we do this?
Is string some special kind of class?
string str = "ABC"; is not assignment. It is construction. Specifically it calls the std::string constructor taking a const char * argument.
It's the same as doing
string str("ABC");
just different syntax.
Assignment also works. You do this:
string str;
str = "ABC";
See also:
Copy initialization
std::string constructors
std::basic_string::operator=
std::basic_string has a constructor like this:
basic_string( const CharT* s, const Allocator& alloc = Allocator() );
Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s.
But the important point to note is that this constructor is not explicit, thus the compiler can do implicit conversion of null terminated character string during constructor call.
For example, following code compiles without any issue:
class Foo {
public:
Foo(int) {}
};
int main() {
Foo f = 10;
}
It won't compile if the constructor is written as:
explicit Foo(int) {}
In C++ a string literal is not a std::string, but a C style character array(char[N]). And yes std::string or any other 3rd party string type that you may see is a class with a converting constructor accepting character arrays as input. More precisely, std::string is a type alias for an instansiation of the template std::basic_string. In short words, before you can do anything with a string literal, you'd better convert it to a string:
std::string{"ABC"}.size()
Or you will have to switch to C API which is not recommended for beginners:
strlen( "ABC")
I want to create a move constructor that takes string literal, and then move that c string to a member pointer.
The best solution I could write is giving a warning:
deprecated conversion from string constant to 'char*' [-Wwrite-strings]
CTextBlock cctb("move(H)");
^
the code:
#include <iostream>
using namespace std;
class CTextBlock
{
public:
CTextBlock(char* &&text)//move constructor
{
pText = text;
}
private:
char *pText;
};
int main()
{
CTextBlock cctb("move(H)"); //WARNING
return 0;
}
First off, the type of string literals is char const[N] (for a suitable constant N). This array can be assigned to a char const* in which case it will decay into a pointer to the first element. It cannot be converted to a char*. Prior to C++11 the conversion to char* was allowed to deal with existing code which wasn't const-correct (e.g., because it started as C code before C got const). This conversion was removed for C++11.
Question is what you actually try to achieve, though: string literals are immutable and persist for the entire life-time of the program. You can just keep as many pointers to them as you want and there is no point in moving pointers as these are entirely cheap to copy.
In your question you indicate that you want to create a move constructor but move constructors take an rvalue reference of the class they are for, e.g., this would be a move constructor for you class:
CTextBlock::CTextBlock(CTextBlock&& other)
: pText(other.pText) {
other.pText = 0;
}
(your class doesn't show any ownership semantics for the pointer pText in which case move construction doesn't really make much sense; the above code assumes that there is some ownership semantics and that a null pointer indicates that the object doesn't own anything).
Just because an argument is constrained to be an rvalue reference doesn't mean that function is a move constructor. All it implies is that the argument is an rvalue an it can reasonably be assume that it's current representation doesn't need to be retained. The string literal appears to be an rvalue because the the string literal is converted into a [temporary] pointer to the start of the array.
A constructor that gets reasonably close to allowing only literals can be realized as a template:
#include <cstddef>
#include <assert>
struct X
{
char const * str;
std::size_t len;
template <std::size_t N>
X(char const (&a)[N]) : str(a), len(N - 1)
{
assert(a[len] == '\0'); // true for string literals
}
};
It's not fool-proof, though, since it will also bind to named character arrays, and the length computation is dubious if your string also contains null values. But if you're trying to avoid accidental use of dynamic values (e.g. in an algorithm that builds strings from expressions and literal strings), this is fairly useful.
A string literal is a const char *. This is true whether or not it's used as an lvalue or an rvalue.
If you review your code again, you are, therefore, attempting to store a const char * into a char *, and that's where your compiler diagnostic is coming from. Your move constructor is taking an rvalue reference to a char *, and not a const char *. Change it to a const char *, and change the pText class member to a const char *, and what you're trying to do should work.
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&);
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.
Does it simply recognize a string literal to be something like a
char*?
This part of the original question wasn't answered as clearly as I'd have liked. I fully endorse (and up-voted) Yossarian's answer for the rest though.
Basically, you need to understand what the compiler is doing when it sees a string literal in the code. That array of chars (as any c-style string really is) is actually stored in a completely different location than the code it's a part of (depending on the architecture, numeric literals can be stored at the location itself as part of the assembly/binary instruction). The two blocks of code here are "more or less" equivalent (ignore lack of includes or namespace declarations) :
int main(void)
{
cout << "Hello!" << endl;
return 0;
}
This is closer to what's "really" happening:
const char HELLO_STR[] = { 'H', 'e', 'l', 'l', 'o', '!', 0 };
int main(void)
{
cout << HELLO_STR << endl;
return 0;
}
Forgive me if I made an error in array init or whatever, but I think this expresses what I mean as for where the string literal is "really" stored. It's not in-line, but is an invisible constant to another part of the program where it's defined. In addition, some (most?) compilers out there also arrange the string literals "together" so that if you have the same literal used in 50 places, it only stores one of them, and all of them refer back to the same constant, saving memory.
So remember that any time you're using a string literal, you're using a const char[N] that exists "invisibly" somewhere, that is implicitly converted to const char*.
Why does the parameter in stringTest have to be marked const when passed a C-Style string?
EDIT:
Temporaries must be immutable. See larsmans comment and answer, he is right.
Simple reason:
void change(std::string& c) { c = "abc"; }
change("test"); // what should the code exactly do??
Furthermore, what would a cstyle string constructor look like, and how does the compiler know to invoke this upon seeing:
It looks up std::string for string(char*) constructor
In the case of something like:
std::string s = "HelloWorld";
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?:
std::string(const std::string&);
No. In this exact case (TYPE variable = SOMETHING), it is the same as writing TYPE variable(SOMETHING);. So, no copying is used.
The const string & s is in this example required to invoke the constructor from the argument "HelloWorld". The constructor used is the type-conversion construction.
A string& s won't do because s is directly referencing a string object.
The type conversion is defined by something similar to
basic_string(const _CharT* __s);
With a typedef
typedef basic_string<char> string;
So the declaration would evaluate to
basic_string(const char * __s)
const char cp[]="jkasdkasjsad";
string a=static_cast<string>(cp);//"const string a" also runs without any error
I have stuck at the above code for the whole afternoon. C++ Primer only give a code like
const char cp[]="jkasdkasjsad";
static_cast<string>(cp);
Could someone tell me is my code legal? Could I call it "cast away const" since no "const" before "string a"?
Any well-defined type conversion, other than those involving low-level const, can be
requested using a static_cast. For example, we can force our expression to use
floating-point division by casting one of the operands to double:
I was confused about the description above, what does "those involing low-level const" mean? Involving at left side or right side of an assignment?
Anyone can save me.. Many thanks!
Your string from cp array is being copied, string variable is not const
const char cp[] = "jkasdkasjsad";
std::string a = static_cast<std::string>(cp);
is equivalent to:
std::string ab = cp;
cp decays to pointer to first element of cp array
There is no real casting at all in this case.
static_cast<string>(cp);
is equivalent to call to string constructor
string(cp);
Temporary variable of type string constructed from cp will be returned from static_cast. Since, I think we talk about std::string, than this constructor will be called
basic_string( const CharT* s,
const Allocator& alloc = Allocator() );
Constructs the string with the contents initialized with a copy of the
null-terminated character string pointed to by s. The length of the
string is determined by the first null character.
Your code is perfectly legal according to the clause 5.2.9/4 of C++ standard:
An expression e can be explicitly converted to a type T using a
static_cast of the form static_cast<T>(e) if the declaration T t(e);
is well-formed, for some invented temporary variable t (8.5). The
effect of such an explicit conversion is the same as performing the
declaration and initialization and then using the temporary variable
as the result of the conversion.
For your example T is std::string, e is cp. There is no casting away constness because of new object creation. Compare with this:
char* p = static_cast<char*>(cp); // error