Literary operator pattern: why not a string?

Once again, in response to another question, I forgot (through my fault) that the literal patterns of the operators are selected from the set of declarations only when integer or floating literals are detected.

As an example:

template <char... C> constexpr int operator "" _x() { return 0; } 

It can be used as 10_x , but it cannot be used either as foo_x or as "foo" _x.

Besides the obvious reason, which is because the standard says that , what is the technical reason (if any) for which they are not considered when with string literals?

I also found a suggestion for this (well, not quite the same, but it gives an idea), but still this is not an option.
What prevents their use for strings?

+5
source share
1 answer

Where there are difficulties associated with character sets and coding, and other such disturbances. What happens if the character set encoding differs between runtime and compilation time, what happens if there are several ways to encode the same character, does the utf-8 encoded string do something different? Do you translate to a character set at runtime or not?

There may be easy answers; but there were enough complications that the developers went β€œwell, we will cut it”, and literals fell into the standard.

The characters allowed for floating point and integral literals, where they are limited, do not have this problem.

But you are related to a document related to the N2750 , and which explicitly stated:

there may be a need for a "raw" form of a string literal in which

 "Hello, " L"Worl\u0044!" 

differs from

 L"Hello, World!" 

but this interacted poorly with the translation phases and there were no convincing cases of using this function.

+6
source

Source: https://habr.com/ru/post/1257548/


All Articles