Reputation: 764
This might be off-topic since there is no code, no output, no anything, but here goes.
The only way I see C++ can know "this"
is a std::string
is because the quotation mark is a language construct, much like the char
between single quotes.
But I would be really surprised if it was the actual answer: does that imply std::string
is the only way C++ has to interpret text between quotation marks? Then the standard lib would not be just an extension, but a part of C++.
What if I want to develop my own MyString
class, and I want to create MyStrings
on the fly using quotation marks: would I be able to do it or is the ""
syntax inevitably linked to the std::string
?
I also thought about ""
being an operator (after all C++ has operator()
), but I couldn't find anything about that.
Upvotes: 5
Views: 1775
Reputation: 3191
There are some good answers here, but none have addressed why the question comes up in the first place. Why would one think "foo"
might have type std::string
?
The answer is std::string
has a constructor from const char *
which will automatically turn the string literal into a std::string
in many situations. C++ has ample mechanisms to allow making library and user defined data structures feel like part of the language in this way. (Operator overloading is another example.)
Consider that e.g. strlen(std::string("foo"))
does not compile, so "foo"
cannot be of type std::string
directly.
Upvotes: 3
Reputation: 48958
Let's clear up some misconceptions. The type of a string literal is a const char[]
, not a std::string
. For example, the type of "this"
would be const char[5]
(there is a null-terminator).
does that imply
std::string
is the only way C++ has to interpret text between quotation marks?
No (it's not std::string
) and there are multiple prefixes for utf-8 strings and wide character strings, like L"wide"
.
then the standard lib would not be just an extension, but a part of C++.
That's true, but not for string literals. You have the type of nullptr
which is std::nullptr_t
. There is also std::byte
that gets special treatment by the standard. A std::initializer_list
constructor is chosen when using list initialization. The result of sizeof
is a std::size_t
and of typeid
it is a std::type_info
. There may be others I can't think of right now.
would I be able to do it or is the
""
syntax inevitably linked to thestd::string
?
It's not a std::string
, but yeah, that's not possible. What you can do however is define a user-defined literal. std::string
has one:
using namespace std::string_literals;
auto string = "this"s;
static_assert(std::is_same_v<decltype(string), std::string>); // ok
Upvotes: 10
Reputation: 73366
"this" is a string literal, not an std::string
.
When you compile your code, Parsing takes action and goes through your code. When it meets enclosing double quotes, it assumes - by convention - that a string literal is found.
If you want to define your own string literals, then check User-defined literals and What new capabilities do user-defined literals add to C++?
PS: I suggest you take a Compiler's course, since if these issues excite you, you will love it.
Upvotes: 1