While skimming over the draft of C++14/C++1y (n3690) I noticed the introduction of the basic_string
litertal suffixes in section §21.7:
inline namespace literals {
inline namespace string_literals {
// 21.7, suffix for basic_string literals:
string operator "" s(const char *str, size_t len);
u16string operator "" s(const char16_t *str, size_t len);
u32string operator "" s(const char32_t *str, size_t len);
wstring operator "" s(const wchar_t *str, size_t len);
}
}
My questions are:
- Is there a possibility to be faster at run-time with
basic_string
literals? - Is my "naive" implementation totally wrong?
- Can the layout of data in ROM be different with
basic_string
literals, or any other difference at compile-time versus run-time?
Background
I know that this allows the direct use of string literals like this:
std::string s1 = "A fabulous string"s;
void sfunc(std::string arg);
int main() {
sfunc("argument"s);
}
But what is the advantage of that over relying on the conversion constructor string(const char*)
?
The "old" code would look:
std::string s1 = "A fabulous string"; // c'tor string(const char*)
void sfunc(std::string arg);
int main() {
sfunc("argument"); // auto-conversion via same c'tor
}
As far as I can see the implementation of operator "" s()
would basically look like this:
std::string operator "" s(const char* lit, size_t sz) {
return std::string(lit, sz);
}
So, just the use of the same c'tor. And my guess is, that has to be done at run-time, am I wrong?
Edit: As Nicol Bolas pointed out correctly below my example does not use the same constructor, but the one with the additional length -- which is very useful for the construction, obviously. This leaves with me the question: Is this better for the compiler to putting string literals into ROM, or something similar at compile-time?
As already stated, the string length is known and automatically passed to the constructor.
No, it's correct.
Probably not, because the relevant
basic_string
constructor is notconstexpr
so won't be eligible for static initialization, so probably can't be put in ROM and has to be done at run-time.