The type of sting literals

Pages: 12
> What do you mean by "decay"?

The implicit conversions that are applied to function arguments when passed by value is called 'decay'.
see: std::decay https://en.cppreference.com/w/cpp/types/decay
I suppose it is called 'decay' because in many cases, there is some loss of type information.

When such a conversion is applied to an array (array to pointer conversion), it is called 'array to pointer decay'


> if you have a type of bounded array

Also for unbounded arrays.

Array-to-pointer conversion
An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. ... The result is a pointer to the first element of the array.
https://eel.is/c++draft/conv.array

I would vote decompose, in the literal sense. As in breaking a composite into the parts from which it is composed, one of which, in this instance, is a raw memory address.

Its a strange word for the process, but that is what it has been called for a very long time so you may as well memorize it and move on.

if you put an s on it, the literal becomes a string object. I didn't see anyone else mention that but I only skimmed the posts.
eg
auto str = "hello"s;
Last edited on
and also:

 
auto str {"hello"sv};


where str becomes a std::string_view object.

Also note, that if you don't have using namespace std, then you need as required:

1
2
using namespace std::string_literals;
using namespace std::string_view_literals;


This is useful if you want to embed a \0 in the string. As \0 terminates a c-string string, it doesn't with string/string_view as these can contain a \0 char.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std::string_view_literals;

int main() {
	const auto str { "qwe\0asd" };
	const auto sv { "qwe\0asd"sv };

	std::cout << str << '\n';
	std::cout << sv << '\n';
}



qwe
qwe asd

Last edited on
Topic archived. No new replies allowed.
Pages: 12